gpt4 book ai didi

c# - 属性更改时未更新列表框项目

转载 作者:太空狗 更新时间:2023-10-29 19:52:28 26 4
gpt4 key购买 nike

当集合的属性发生变化时,我在更新包含 ObservableCollection 的列表框时遇到问题(从列表中添加/删除项目工作正常):

列表框已设置ItemsSource="{Binding Path=AllPerson}"代码隐藏中的数据上下文设置如下 this.DataContext = allPersonClass; .

allPersonClass包含 ObservableCollection<Person> allPerson

Person包含名称等属性。

我已经覆盖了人的 ToString返回方法 Name属性使 listBox 显示有效数据

我试过制作 Person实现 INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;
private void onPropertyChanged(object sender, string propertyName) {
if (this.PropertyChanged != null) {
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}

public string Name {
get { return name; }
set {
name = value;
onPropertyChanged(this, "allPersonClass");
}
}

并且在每个属性 setter 中都有 onPropertyChanged(this, "propertyName");已执行但 listBox 永远不会更新已创建的项目

知道哪里出了问题吗?

这是带有 listBox xaml 的窗口

     <Button x:Name="btnDetail" Content="Detail" HorizontalAlignment="Left" Margin="361,249,0,0" VerticalAlignment="Top" Width="75" Click="ButtonDetailClick"/>
<ListBox x:Name="listPerson" ItemsSource="{Binding Path=AllPerson}" HorizontalAlignment="Left" Height="170" Margin="33,29,0,0" VerticalAlignment="Top" Width="155" IsSynchronizedWithCurrentItem="True"/>
<Button x:Name="btnLoad" Content="Load" HorizontalAlignment="Left" Margin="58,249,0,0" VerticalAlignment="Top" Width="75" Click="btnLoad_Click"/>
<Button x:Name="btnSave" Content="Save" HorizontalAlignment="Left" Margin="138,249,0,0" VerticalAlignment="Top" Width="75" Click="ButtonSaveClick"/>

这是进行修改的 DetailView 窗口的一部分(绑定(bind)到 Person)

<TextBox Text="{Binding Path=Name}" Height="23" HorizontalAlignment="Left" Margin="118,20,0,0" Name="txtName" VerticalAlignment="Top" Width="141" />

这是 AllPersonClass 的一部分:

public class AllPersonClass {
private ObservableCollection<Person> allPerson;

public AllPersonClass() {
allPerson = new ObservableCollection<Person>();
}

public ObservableCollection<Person> AllPerson {
get { return allPerson; }
set { allPerson = value; }
}

public void addPerson(Person newPerson) {
allPerson.Add(newPerson);
}


public Person getPerson(int personIndex) {
return allPerson[personIndex];
}
}

编辑

这里是在详细 View 中保存更改的方法的相关部分

private void OnBtnSaveClick(object sender, RoutedEventArgs e) {
person.Name = txtName.Text;
person.SurName = txtSurName.Text;
}

请注意,在“ObservableCollection allPerson”中进行了更改,只有 listBox 继续显示旧数据

最佳答案

如果您希望在更改 Person 属性时更新 UI,您必须通知该属性已更改,而不是类

public string Name 
{
get { return name; }
set
{
name = value;
onPropertyChanged(this, "allPersonClass");
}
}

<TextBox Text="{Binding Path=Name}" .....

应该是

public string Name 
{
get { return name; }
set
{
name = value;
onPropertyChanged(this, "Name");
}
}

<TextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" .....

而且您不必覆盖 Person 模型上的 ToString() 即可在您的 ListBox 中正确显示,您可以设置 ListBox DisplayMemberPathListBox

中显示你想要的属性
  <ListBox ItemsSource="{Binding Path=AllPerson}" DisplayMemberPath="Name" />

编辑:回答您的评论问题:

public string Name 
{
get { return name; }
set
{
name = value;
onPropertyChanged(this, "Name");
onPropertyChanged(this, "Display");
}
}

public string Surname
{
get { return surname; }
set
{
surname= value;
onPropertyChanged(this, "Surname");
onPropertyChanged(this, "Display");
}
}

public string Display
{
get { return Name + " " + Surname; }
}

关于c# - 属性更改时未更新列表框项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15396786/

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