wpf INotifyCollectionChanged 不更新 UI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16297983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
INotifyCollectionChanged is not updating the UI
提问by Vikram
I have a class as shown below. All the functions I have removed for brevity
我有一个类,如下所示。为简洁起见,我删除的所有功能
public class PersonCollection:IList<Person>
{}
Now I have one more Model class as shown below. AddValueCommand is class deriving from ICommand which again I am omiting.
现在我还有一个 Model 类,如下所示。AddValueCommand 是从 ICommand 派生的类,我再次省略了它。
public class DataContextClass:INotifyCollectionChanged
{
private PersonCollection personCollection = PersonCollection.GetInstance();
public IList<Person> ListOfPerson
{
get
{
return personCollection;
}
}
public void AddPerson(Person person)
{
personCollection.Add(person);
OnCollectionChanged(NotifyCollectionChangedAction.Reset);
}
public event NotifyCollectionChangedEventHandler CollectionChanged = delegate { };
public void OnCollectionChanged(NotifyCollectionChangedAction action)
{
if (CollectionChanged != null)
{
CollectionChanged(this, new NotifyCollectionChangedEventArgs(action));
}
}
ICommand addValueCommand;
public ICommand AddValueCommand
{
get
{
if (addValueCommand == null)
{
addValueCommand = new AddValueCommand(p => this.AddPerson(new Person { Name = "Ashish"}));
}
return addValueCommand;
}
}
}
In the main window I am binding my UI to Model as shown below
在主窗口中,我将 UI 绑定到模型,如下所示
DataContextClass contextclass = new DataContextClass();
this.DataContext = new DataContextClass();
And My UI looks like as shown below
我的用户界面如下所示
<ListBox Margin="5,39,308,113" ItemsSource="{Binding Path=ListOfPerson}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Height="20" Text="{Binding Path=Name}"></TextBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Button" HorizontalAlignment="Left" Command="{Binding Path=AddValueCommand}" Margin="233,39,0,73" />
My List Box is not updating with the new value when button is clicked. What I am missing here.
单击按钮时,我的列表框未使用新值更新。我在这里缺少什么。
回答by VS1
Instead of using IListuse ObservableCollection<T>and define your PersonCollectionclass as below:
而不是使用IListuseObservableCollection<T>并定义您的PersonCollection类,如下所示:
public class PersonCollection : ObservableCollection<Person>
{}
You can read more about ObservableCollection<T>class herewhich is designed specifically for collection change notifications in WPF DataBinding scenarios.
您可以在此处阅读有关ObservableCollection<T>类的更多信息,该类专为 WPF 数据绑定方案中的集合更改通知而设计。
As you can see from the definition in MSDNbelow, it already implements INotifyCollectionChanged
从下面MSDN中的定义可以看出,它已经实现了INotifyCollectionChanged
public class ObservableCollection<T> : Collection<T>,
INotifyCollectionChanged, INotifyPropertyChanged
More articles to help you with usage of ObservableCollection class in WPF are below:
更多帮助您在 WPF 中使用 ObservableCollection 类的文章如下:
Create and Bind to an ObservableCollection
An introduction to ObservableCollection in Wpf
Databinding a ObservableCollection in MVVM
创建并绑定到一个 ObservableCollection
Wpf 中的 ObservableCollection介绍
Databinding a ObservableCollection in MVVM
回答by Daniel Hilgarth
INotifyCollectionChangedhas to be implemented by a collection class. Not by the class containingthe collection.
You need to remove INotifyPropertyChangedfrom DataContextClassand add it to PersonCollection.
INotifyCollectionChanged必须由集合类实现。不是包含集合的类。
您需要删除INotifyPropertyChanged的DataContextClass,并把它添加到PersonCollection。

