问题
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