在 MVVM 中,刷新属性 NumberOfAdults 和 NumberOfChildren 以在 UI 上更新的意图是什么?更一般地说,People 视图模型如何捕获更新并刷新依赖于内容的属性OberservableCollection<Person>
?
该解决方案需要更高级地使用 XAML 绑定语法。它是什么?
人员视图模型
public class Person : INotifyPropertyChanged
{
private int _age;
public int Age
{
get { return _age; }
set { _age = value; NotifyPropertyChanged("Age"); }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; NotifyPropertyChanged("Name"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
人员视图模型
using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
public class PeopleViewModel : INotifyPropertyChanged
{
public PeopleViewModel()
{
People.Add(new Person { Name = "Happy", Age = 12 });
People.Add(new Person { Name = "Sleepy", Age = 15 });
People.Add(new Person { Name = "Sneezy", Age = 17 });
People.Add(new Person { Name = "Grumpy", Age = 45 });
People.Add(new Person { Name = "Dopey", Age = 50 });
People.Add(new Person { Name = "Bashful", Age = 60 });
People.Add(new Person { Name = "Doc", Age = 75 });
}
private ObservableCollection<Person> _people = new ObservableCollection<Person>();
public ObservableCollection<Person> People
{
get { return _people;}
set { _people = value;}
}
public int NumberOfAdults
{
get
{
int count = 0;
foreach (Person p in People)
{
if (p.Age >= 18)
{
count += 1;
}
}
return count;
}
}
public int NumberOfChildren
{
get
{
int count = 0;
foreach (Person p in People)
{
if (p.Age < 18)
{
count += 1;
}
}
return count;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
主要 XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Label Content="{Binding Path=NumberOfAdults}"></Label>
<Label Content="{Binding Path=NumberOfChildren}"></Label>
<DataGrid ItemsSource="{Binding Path=People}"></DataGrid>
</StackPanel>
</Window>