【问题标题】:WPF: static INotifyPropertyChanged eventWPF:静态 INotifyPropertyChanged 事件
【发布时间】:2015-06-15 09:23:07
【问题描述】:

这是我的模型:

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 的数量。 那么可以为我的static 变量添加另一个PropertyChanged Event 吗?

【问题讨论】:

标签: wpf binding


【解决方案1】:

你应该有一个包含 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 文章中的绑定到集合部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-14
    • 2010-11-13
    • 1970-01-01
    • 2021-09-07
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多