我的中有一個,它與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方式來處理這個問題?