I have a window in my application with following resources:
<Window.Resources>
<ResourceDictionary>
<Data:IssueRecords x:Key="DataSource"/>
<CollectionViewSource x:Key="DataCollection" Source="{StaticResource DataSource}"
Filter="CollectionViewSource_Filter">
</CollectionViewSource>
</ResourceDictionary>
</Window.Resources>
There is a standard event handler – a method, called CollectionViewSource_Filter and DataGrid, to apply filter to. After my window loads, everything works perfectly, including filters.
For applying filters, I call a ReloadGrid method…
private void ReloadGrid(object sender, RoutedEventArgs e)
{
CollectionViewSource.GetDefaultView(GridData.ItemsSource).Refresh();
}
But, when user does any action, which makes changes to my database (delete, modify or create new), I need to reload those data sources, so I call…
private void ReloadDataSources()
{
var dataSource = this.FindResource("DataSource") as IStockRecords;
dataSource.ReloadData();
var dataCollection = this.FindResource("DataCollection") as CollectionViewSource;
dataCollection = new CollectionViewSource() { Source = dataSource };
dataCollection.Filter += new FilterEventHandler(CollectionViewSource_Filter);
Binding binding = new Binding() { Source = dataCollection };
BindingOperations.SetBinding(GridData, DataGrid.ItemsSourceProperty, binding);
}
I think, I do everything, what is needed to read actual data from database and reload the datasources in my window. But when I use any filter, after I call ReloadDataSources(), the filter event is not being used anymore. I debugged a source code and Refresh method doesn’t invoke CollectionViewSource_Filter, even when I set FilterEventHandler…
Am i missing anything?
Thanks, JiKra
OK, there seems to be a problem when I recreated CollectionViewSource object. So, the final version is…
Thank you both for your effort…
JiKra