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
中的所有 Persons
属性,我想添加 Counter
字段来保存 [=15] 的数量=] 我有。
那么可以为我的 static
变量添加另一个 PropertyChanged Event
吗?
你应该有一个包含 Person 对象集合的视图模型而不是静态计数器
public class ViewModel
{
public ObservableCollection<Person> Persons { get; set; }
}
并将 ListView 的 ItemsSource
属性 绑定到此集合。
<ListView ItemsSource="{Binding Persons}">
...
</ListView>
您现在可以绑定到集合的 Count
属性 以获取元素数:
<TextBlock Text="{Binding Persons.Count}"/>
如需进一步阅读,请参阅 MSDN 上 Data Binding Overview 文章中的 绑定到集合 部分。
这是我的模型:
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
字段来保存 [=15] 的数量=] 我有。
那么可以为我的 static
变量添加另一个 PropertyChanged Event
吗?
你应该有一个包含 Person 对象集合的视图模型而不是静态计数器
public class ViewModel
{
public ObservableCollection<Person> Persons { get; set; }
}
并将 ListView 的 ItemsSource
属性 绑定到此集合。
<ListView ItemsSource="{Binding Persons}">
...
</ListView>
您现在可以绑定到集合的 Count
属性 以获取元素数:
<TextBlock Text="{Binding Persons.Count}"/>
如需进一步阅读,请参阅 MSDN 上 Data Binding Overview 文章中的 绑定到集合 部分。