INotifyPropertyChanged WPF中的计数属性? [英] INotifyPropertyChanged for Count property in WPF?
问题描述
我有一个文本框,该文本框绑定到我的视图模型中的Int
属性,该属性获取ObservableCollection
的计数.这是我的Xaml绑定:
I have a textbox which is bound to an Int
property in my viewmodel which gets a count of an ObservableCollection
. Here is my Xaml binding:
Text="{Binding ArticleCount, Mode=OneWay}"
还有我的int属性:
private Int32 _ArticleCount;
public int ArticleCount
{
get
{
if (this.ModelviewArticleObservableList == null)
{
return 0;
}
else
{
return this.ModelviewArticleObservableList.Count;
}
}
}
这在应用程序初始化时效果很好,我在UI TextBox
中得到了ObservableCollection
计数.但是,有些方法随后会更改ObservableCollection
,因此计数也会发生变化.我需要一个Property Changed事件来通知计数更改的时间,但是我不确定如何执行此操作?这是我的ObservableCollection
属性:
This works fine on application initialisation and I get the ObservableCollection
count in my UI TextBox
. However there are methods that then change the ObservableCollection
and therefore the count changes. I need a Property Changed event to notify the when the count has changed but I'm not sure on how to do this? Here is my ObservableCollection
property:
private ObservableCollection<viewArticle> _ModelviewArticleObservableList = new ObservableCollection<viewArticle>();
public ObservableCollection<viewArticle> ModelviewArticleObservableList
{
get { return _ModelviewArticleObservableList; }
set
{
_ModelviewArticleObservableList = value;
OnPropertyChanged("ModelviewArticleObservableList");
}
}
我目前可以正常使用INPC:
I currently have a normal implementation of INPC:
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
推荐答案
每次集合中元素的数量发生更改时,都必须引发PropertyChanged
事件.一种好方法是订阅 CollectionChanged
事件.
You have to raise the PropertyChanged
event every time the number of elements in the collections changes. A good way is to subscribe to the CollectionChanged
event of the ObservableCollection
.
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// you could examine e to check if there is an actual change in the count first
OnPropertyChanged("ArticleCount");
}
此外,有时会将集合本身设置为null
或其他值,在这种情况下,您的ArticleCount
属性也应更改.您还必须从旧集合中删除CollectionChanged
事件处理程序,并将其添加到新集合中.
Additionally there is the case that the collection itself is set to null
or a different value, in which case your ArticleCount
property should change too. You also have to remove your CollectionChanged
event handler from the old collection and add it to the new one.
public ObservableCollection<viewArticle> ModelviewArticleObservableList
{
get { return _ModelviewArticleObservableList; }
set
{
if (_ModelviewArticleObservableList != null)
{
_ModelviewArticleObservableList.CollectionChanged -= OnCollectionChanged;
}
_ModelviewArticleObservableList = value;
if (_ModelviewArticleObservableList != null)
{
_ModelviewArticleObservableList.CollectionChanged += OnCollectionChanged;
}
OnPropertyChanged("ModelviewArticleObservableList");
OnPropertyChanged("ArticleCount");
}
}
这篇关于INotifyPropertyChanged WPF中的计数属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!