问题:
- 在视图中声明了一个
ItemsControl
(或从ItemsControl
派生的控件)。 - 将
ItemsControl.ItemsSource
属性绑定到ViewModel中的ObservableCollection
。 - 当添加/删除
ObservableCollection
中的项时,您的视图会按预期更新。 - 但是,当您更改
ObservableCollection
中项目的属性时,视图不会更新。
背景信息:
看起来这是许多WPF开发人员遇到的常见问题。已经有人问过几次:
ObservableCollection在其中更改项时没有注意到(即使使用INotifyPropertyChanged)
ObservableCollection和Item PropertyChanged
我的实现:
我尝试实现Notify ObservableCollection when Item changes中的被接受的解决方案。基本思想是在MainWindowViewModel中为ObservableCollection
的每个项连接一个PropertyChanged
处理程序。当更改项的属性时,事件处理程序将被调用并且以某种方式更新视图。
我无法让实现工作。这是我的实施方式。
ViewModels:
class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
项目视图模型:
class EmployeeViewModel : ViewModelBase
{
private int _age;
private string _name;
public int Age
{
get { return _age; }
set
{
_age = value;
RaisePropertyChanged("Age");
}
}
public string Name
{
get { return _name; }
set
{
_name = value;
RaisePropertyChanged("Name");
}
}
public override string ToString()
{
return string.Format("{0} is {1} years old", Name, Age);
}
}
主窗口视图模型:class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<EmployeeViewModel> _collection;
public MainWindowViewModel()
{
_collection = new ObservableCollection<EmployeeViewModel>();
_collection.CollectionChanged += MyItemsSource_CollectionChanged;
AddEmployeeCommand = new DelegateCommand(() => AddEmployee());
IncrementEmployeeAgeCommand = new DelegateCommand(() => IncrementEmployeeAge());
}
public ObservableCollection<EmployeeViewModel> Employees
{
get { return _collection; }
}
public ICommand AddEmployeeCommand { get; set; }
public ICommand IncrementEmployeeAgeCommand { get; set; }
public void AddEmployee()
{
_collection.Add(new EmployeeViewModel()
{
Age = 1,
Name = "Random Joe",
});
}
public void IncrementEmployeeAge()
{
foreach (var item in _collection)
{
item.Age++;
}
}
private void MyItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
foreach (EmployeeViewModel item in e.NewItems)
item.PropertyChanged += ItemPropertyChanged;
if (e.OldItems != null)
foreach (EmployeeViewModel item in e.OldItems)
item.PropertyChanged -= ItemPropertyChanged;
}
private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
RaisePropertyChanged("Employees");
}
}
查看:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:d="clr-namespace:Iress.IosPlus.DynamicOE.Controls"
Title="MainWindow" Height="350" Width="350">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"></ColumnDefinition>
<ColumnDefinition Width="0.7*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<Button Command="{Binding AddEmployeeCommand}">Add Employee</Button>
<Button Command="{Binding IncrementEmployeeAgeCommand}">Increment Employee Age</Button>
</StackPanel>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=Employees[0]}"></TextBlock>
<ItemsControl Grid.Row="1" ItemsSource="{Binding Path=Employees}" BorderBrush="Red" BorderThickness="1"></ItemsControl>
</Grid>
</Grid>
我的结果:
为了验证我的实现,我创建了一个视图。 TextBlock.Text
绑定到集合的第一个项目。 ItemsControl
绑定到集合本身。
- 按下“添加员工”按钮会在集合中添加一个
EmployeeViewModel
对象,TextBlock
和ItemsControl
都将按预期更新。 - 再次按下“添加员工”,
ItemsControl
将更新为另一个条目。太好了! - 按下“增加员工年龄”按钮,每个项目的
Age
属性都会增加 1。PropertyChanged
事件被触发。调用ItemPropertyChanged
事件处理程序。Textblock
将按预期更新。然而,ItemsControl
没有更新。
根据在Notify ObservableCollection when Item changes中的答案,我认为当 Employee.Age
更改时,ItemsControl
也应该更新。
ItemsControl
能够刷新。 - Frank Liu