WPF:静态INotifyPropertyChanged的事件 [英] WPF: static INotifyPropertyChanged event
问题描述
这是我的模型:
class Person : INotifyPropertyChanged
{
public static int Counter;
public string _firstName;
public string _lastName;
public event PropertyChangedEventHandler PropertyChanged;
public string FirstName
{
get {return _firstname; }
set
{
_fileName = value;
NotifyPropertyChange("FirstName");
}
}
public AddPerson(Person person)
{
Counter++;
}
}
我有这样的 NotifyPropertyChange
,改变了我所有的我的
的ListView <内部人士
属性/ code>,我想添加持有
,我有。对象数计数器
字段
那么,是否可以添加其他 PropertyChanged事件
我的静态
变量?
I have this NotifyPropertyChange
that changed all my Persons
properties inside my ListView
and i want to add the Counter
field that hold the number of Objects
that i have.
So is it possible to add another PropertyChanged Event
for my static
variable ?
推荐答案
而不是一个静态的柜台,你应该有Person对象的集合视图模型
Instead of a static counter you should have a view model with a collection of Person objects
public class ViewModel
{
public ObservableCollection<Person> Persons { get; set; }
}
和绑定ListView控件到这个集合的的ItemsSource
属性。
and bind the ItemsSource
property of the ListView to this collection.
<ListView ItemsSource="{Binding Persons}">
...
</ListView>
您现在可以绑定到计数
集合的属性来获取元素的数目:
You could now bind to the Count
property of the collection to get the number of elements:
<TextBlock Text="{Binding Persons.Count}"/>
有关进一步阅读看到的绑定到集合的中的数据绑定概述 MSDN上的文章。
For further reading see the Binding to Collections section in the Data Binding Overview article on MSDN.
这篇关于WPF:静态INotifyPropertyChanged的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!