这是我的模型:
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中所有的Persons属性,我想添加一个Counter字段,这个字段保存了我拥有的Objects的数量。那么,是否可以为我的PropertyChanged Event变量添加另一个static呢?
发布于 2015-06-15 09:32:51
而不是静态计数器,您应该有一个带有Person对象集合的视图模型。
public class ViewModel
{
public ObservableCollection<Person> Persons { get; set; }
}并将ListView的ListView属性绑定到此集合。
<ListView ItemsSource="{Binding Persons}">
...
</ListView>现在可以绑定到集合的Count属性以获取元素的数量:
<TextBlock Text="{Binding Persons.Count}"/>有关进一步的阅读,请参阅MSDN上的数据绑定概述文章中到集合的绑定部分。
https://stackoverflow.com/questions/30841598
复制相似问题