我的中有一个,它与ObservableCollection
绑定。这是一个搜索结果DataGrid
。问题是,我更新搜索结果ObservableCollection
后,实际DataGrid
未更新。WPF ObservableCollection在创建新的ObservableCollection时未更新DataGrid
之前,我得到了投什么,请注意这不是关于在列中的数据(即绑定完美的作品)是有关清除,然后把完全新的数据到ObservableCollection
未更新DataGrid
。 So linking to something like this will not help as my properties are working correctly
背景:
的ObservableCollection
在ViewModel
这样被声明;
public ObservableCollection<MyData> SearchCollection { get; set; }
绑定到我的搜索ObservableCollection
这样的搜索DataGrid
;
<DataGrid ItemsSource="{Binding SearchCollection}" />
在ViewModel
我有一个像这样的搜索方法;
var results =
from x in MainCollection
where x.ID.Contains(SearchValue)
select x;
SearchCollection = new ObservableCollection<MyData>(results);
该方法正确激发并产生所需结果。然而DataGrid
没有更新新的结果。我知道ViewModel
具有正确的数据,因为如果我在页面上放置按钮并在click事件中放置此代码;
private void selectPromoButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var vm = (MyViewModel)DataContext;
MyDataGrid.ItemsSource = vm.SearchCollection;
}
DataGrid
现在可以正确显示结果。
我知道我可以把一些事件放在页面后面的代码中,但是不会打败MVVM吗?什么是正确的MVVM方式来处理这个问题?