I can't believe I am stumped with this fairly straightforward thing. I originally posted this in the WPF datagrid codeplex discussions but got no response. So I am trying here:
I can't seem to find a way to do this. I create a datagrid whose datacontext is initially filled with rows from a table. I have implemented a search functionality that will return some rows based on a condition. Suppose I want to display only those rows, how do I destroy the initially created datacontext and add the newly filtered collection?
I naively started doing it like this:
(Late Edit: I can't seem to type Generics code here -- the cast in the following line is suppoed to cast datagrid.Items to MyType (for example))
IEnumerable rows = datagrid.Items.Cast();
IEnumerable filteredRows = rows.Where(row => row.someCondition == true);
how do I now make my datagrid display only the filteredRows? Just doing:
datagrid.DataContext = null;
datagrid.DataContext = filteredRows;
doesn't work (it even smells stupid for some reason).
I also need to do the reverse (once I get this working). Some buttonclick should allow the user to "clear" the search results and re-plug the DataContext back to "rows" (in the above snippet).
What am I missing?