WPF: static INotifyPropertyChanged event

我们两清 提交于 2020-01-22 03:24:30

问题


This is my model:

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++;
   }
}

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 ?


回答1:


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; }
}

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}"/>

For further reading see the Binding to Collections section in the Data Binding Overview article on MSDN.



来源:https://stackoverflow.com/questions/30841598/wpf-static-inotifypropertychanged-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!