gpt4 book ai didi

WPF RadioButton 已知问题 : why the old property value seems still binding after property changed

转载 作者:行者123 更新时间:2023-12-02 01:55:26 25 4
gpt4 key购买 nike

谢谢。我希望我可以上传整个解决方案,因为我在打开这个问题之前已经准备好了。但我没有找到如何。我将在此处尽可能复制所有代码。

The question is: I think this is an easy replicated issue within WPF. I made a simple version of code to explain my question clearly. The container 'TwoPersons' in the sample has two instances of class 'Person'. The 'Current' property will be data context of a detail panel which populates person's detail info. I can use Current setter method to jump between different persions. I can see the details changing accordingly. The question is during the switching and populating current person, UI controls changes (like change male to female), then the change affects PREVIOUS item it was binding. The persion instance will be modified unexpectly. It seems previous instance still has connection with the view after new instance bound. How can I avoid this situation. Any help will be appreciated.



================================================== ======================

这是 MainWindow.xaml.cs
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
TwoPersons twoPersons = new TwoPersons();
public MainWindow()
{
InitializeComponent();
DataContext = twoPersons;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
twoPersons.Next();
}
}

两个人的容器:
public class TwoPersons : INotifyPropertyChanged
{
Person p1 = new Person("Mary") { IsFemale = true };
Person p2 = new Person("Luis") { IsMale = true };

Person current;

public Person Current
{
get
{
if (current == null)
current = p1;

return current;
}

set
{
if (current != value)
{
current = value;
OnPropertyChanged("Current");
}
}
}

public void Next()
{
Current = Current == p1 ? p2 : p1;
}

private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, e);
}
}

public event PropertyChangedEventHandler PropertyChanged;
}

人物类:
public class Person : INotifyPropertyChanged
{
public Person(string name)
{
this.name = name;
}

private string name;
public string Name
{
get { return name; }
}

public bool isMale;
public bool IsMale
{
get { return isMale; }
set
{
if (isMale != value)
{
isMale = value;
OnPropertyChanged("IsMale");
OnPropertyChanged("IsFemale");
OnPropertyChanged("Description");
}
}
}

public bool IsFemale
{
get { return !isMale; }
set { IsMale = !value; }
}

public string Description
{
get
{
return isMale ? "Male" : "Female";
}
}

private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, e);
}
}

public event PropertyChangedEventHandler PropertyChanged;
}

主窗口xaml
<Grid>
<StackPanel DataContext="{Binding Current}">
<TextBlock Text="{Binding Name}"></TextBlock>
<RadioButton IsChecked="{Binding IsMale}">Male</RadioButton>
<RadioButton IsChecked="{Binding IsFemale}">Female</RadioButton>
<TextBlock Text="{Binding Description}"></TextBlock>
<Button Click="Button_Click" Height="32">Next >></Button>
<TextBlock>Note. Mary should be alway a female! But it changes when navigating by clicking the button above </TextBlock>
</StackPanel>
</Grid>

最佳答案

当您在 StackOverflow 上提出问题时,您确实应该提供重现问题所需的所有代码。您应该这样做的第一个原因是,试图帮助您的人不必浪费时间手动重现您的代码。

第二个更重要的原因是,当我不得不用我自己的代码重现你的错误时(对于你没有显示的代码位,例如 Person 类)然后我已经正确地实现了代码,所以你的示例代码对我来说很好用。

因此,我只能假设您的 Person 中有错误。类,但由于您没有显示,我无法确认。我最好的猜测是你只是没有实现 INotifyPropertyChangedPerson 中正确类(class),甚至可能是您的主要类(class)。当你这样做时,你应该发现你的代码工作得很好。

关于WPF RadioButton 已知问题 : why the old property value seems still binding after property changed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20398467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com