【发布时间】:2014-04-10 07:35:37
【问题描述】:
我有一个理论问题。
<ListBox ItemSource= "{Binding Fruits}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Color}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox
假设我的 ViewModel 中有 ObservableCollection<Fruit> Fruits。
水果不是我的课,所以当水果颜色发生变化时我无法实现INotifyPropertyChanged。
当这些属性发生更改时,我在我的 ViewModel 中知道:
public ChangeColor()
{
Fruits[1].Color = "Blue";
//Notify Fruits Here, How?
}
- 怎么做?
- 在后台实现
INotifyPropertyChanged时它是如何工作的?如果我有ObservableCollection<Fruit> Fruits并且 Fruit Color 属性发生更改,则会发送类似NotifyPropertyChanged("Fruits.Name")的内容?
【问题讨论】:
-
将您的
Fruit包装在封装 Fruit 并添加 INotifyPropertyChanged-Functionality 的Fruit中,以便在颜色更改时引发 PropertyChangedEvent -
另外,您可以查看面向方面的编程 (AOP),只需 google POSTSHARP。根据上面的建议,将你的 M 包装在 VM 中的方法要长得多。
-
好的,没错。但是第二个问题呢?如何通过在 ViewModel 中调用事件来更新 ItemsSource 中的每种颜色?
标签: wpf binding inotifypropertychanged