【发布时间】:2014-12-12 09:43:32
【问题描述】:
我正在使用 MVVM 模式,在我看来我有这个dataGrid:
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource myMultiValueConverter}">
<MultiBinding.Bindings>
<Binding />
<Binding ElementName="ThisControl" Path="DataContext.MyObservableCollectionInViewModel"/>
<Binding ElementName="thisControl" Path="DataContext.ControlType"/>
<Binding ElementName="ThisControl" Path="DataContext.ExternalItems"/>
<Binding Path="Componentes.OneProperty"/>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
我的视图模型有这个代码:
private void myMethod()
{
MyObservableCollectionInViewModel.Clear();
MyObservableCollectionViewModel.Add(new myType());
}
当我执行MyMethod()方法时,如果我没记错,多值转换器会运行,因为ObservableCollection在我添加或删除项目时实现INotifyPropertyChanged,但在这种情况下不起作用.
但是我的dataGrid 的DataSource 有另一个ObservableCollection,它按预期工作,当我从ObservableCollection 添加或删除项目时刷新dataGrid。
但是,如果在myMethod 我这样做:
private myMethod()
{
myObservableCollectionInMyViewModel.Clear();
myObservableCollectionInMyViewModel.Add(new MyType());
myObservableCollectionInMyViewModel = new ObservableCollection(myObservableCollectionInMyViewModel);
}
它可以工作,所以当我创建一个新的ObservableCollection 时,视图会得到通知,而不是当我在实际的ObservableCollecion 中添加或删除项目时。
【问题讨论】:
标签: c# mvvm datagrid observablecollection imultivalueconverter