WPF:静态INotifyPropertyChanged的事件 [英] WPF: static INotifyPropertyChanged event

查看:827
本文介绍了WPF:静态INotifyPropertyChanged的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模型:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆