I have a DataGrid
that is bound to an ObservableCollection
in the ViewModel
. This is a search results DataGrid
. The problem is that after I update the search results ObservableCollection
the actual DataGrid
is not updated.
Before I get down voted to nothing, please note this is NOT about data in the columns (that bind works perfectly) it is about clearing and then placing completely new data into ObservableCollection
that is not updating the DataGrid
. So linking to something like this will not help as my properties are working correctly
Background:
The ObservableCollection
is declared in the ViewModel
like this;
public ObservableCollection<MyData> SearchCollection { get; set; }
The search DataGrid
that is bound to my search ObservableCollection
like this;
<DataGrid ItemsSource="{Binding SearchCollection}" />
In the ViewModel
I have a search method like this;
var results =
from x in MainCollection
where x.ID.Contains(SearchValue)
select x;
SearchCollection = new ObservableCollection<MyData>(results);
The method fires correctly and produces the desired results. Yet the DataGrid
is not updated with the new results. I know the ViewModel
has the correct data because if I put a button on the Page and in the click event put this code;
private void selectPromoButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var vm = (MyViewModel)DataContext;
MyDataGrid.ItemsSource = vm.SearchCollection;
}
The DataGrid
now correctly shows the results.
I know I could put some event in the code behind of the Page but wouldn't that defeat MVVM? What is the correct MVVM way to handle this?