gpt4 book ai didi

c# - ListView 未更新项目源 ObservableCollection 项目属性更改

转载 作者:行者123 更新时间:2023-11-30 14:53:56 25 4
gpt4 key购买 nike

考虑以下示例代码:

View .xml

<Grid>
<ListView Name="NameList" HorizontalAlignment="Left" Height="142" Margin="55,45,0,0" VerticalAlignment="Top" Width="389">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding FirstName}"/>
<Label Content="{Binding LastName}" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Content="Button" HorizontalAlignment="Left" Margin="120,256,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

</Grid>

View.xml.cs

public partial class MainWindow : Window
{
ViewModel vm;
public MainWindow()
{
InitializeComponent();
vm = new ViewModel();
this.DataContext = vm;
NameList.ItemsSource = vm.fn;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
vm.fn.Add(new fullname("P", "Q"));
vm.fn[0].FirstName = "NewName";
}

}

ViewModel.cs

class ViewModel : INotifyPropertyChanged
{

public ViewModel()
{
fn = new ObservableCollection<fullname>();
fn.CollectionChanged += ContentCollectionChanged;
fn.Add(new fullname("A", "B"));
fn.Add(new fullname("C", "D"));
}

public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (fullname item in e.OldItems)
{
//Removed items
item.PropertyChanged -= EntityViewModelPropertyChanged;
}
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (fullname item in e.NewItems)
{
//Added items
item.PropertyChanged += EntityViewModelPropertyChanged;
}
}
}

public void EntityViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
//This will get called when the property of an object inside the collection changes
}

public ObservableCollection<fullname> _fn;
public ObservableCollection<fullname> fn
{
get { return _fn; }
set { _fn = value; OnPropertyChanged("fn"); }
}

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{

}
}
}

模型.cs

class fullname : INotifyPropertyChanged
{
public fullname(string f, string l)
{
FirstName = f;
LastName = l;
}

public string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; OnPropertyChanged("FirstName"); }
}


public string _LastName;
public string LastName
{
get { return _LastName; }
set { _LastName = value; OnPropertyChanged("LastName"); }
}

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{

}
}
}

如果我在 ObservableCollection 中添加或删除任何项目,它会正确更新 View 中的 ListView,但问题是如果我修改 ObservableCollection 项目属性,ListView 不会更新。

例如:单击上面指定的按钮
A。添加一个新项目(成功反射(reflect)在ListView中)
b.修改第一项的名字(不反射(reflect)在 ListView 中)

我应该怎么做才能使修改反射(reflect)在 View 中。

如果有人能指出我做错了什么,我将不胜感激。

最佳答案

ObservableCollection 事件不会在项目更改时触发,仅在添加、删除或移动时触发。您必须在项目类中实现 INotifyPropertyChanged 接口(interface),并在集合中注册每个项目事件。

参见 here

如果你在集合中做了很大的改动,你可以提出一个

NotifyCollectionChangedAction.Reset

重置它们的事件

参见 here

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

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