Stopping controls using sharing filters

I have two ItemsControls, one ListView and one custom control that I am developing.

I set the ItemsControl.ItemsSource property of both controls to the same IEnumerable, in this case List.

I apply a filter to the ItemsControl.Items property of my custom control (this.Items.Filter = myFilter), and my control is updated as expected, showing only the items matching the filter.

However, the ListView, using the same IEnumerable for the ItemsControl.ItemsSource property, is also updated to show only those items that match the filter that I applied to my custom control.

Can someone tell me how to save the filter in my custom control by affecting the items in my list?

+4
source share
2 answers

The first thing I can come up with is that it does not require any big changes in what you have described, just to wrap the ItemsSource collections in the CollectionViewSource in your XAML where they are assigned.

<DockPanel> <Button DockPanel.Dock="Top" Content="Filter Lowercase Names" Click="OnFilterClick"/> <ListView x:Name="uiListView"> <ListView.Resources> <CollectionViewSource x:Key="ItemsCollection" Source="{Binding Names}" /> </ListView.Resources> <ListView.ItemsSource> <Binding Source="{StaticResource ItemsCollection}" /> </ListView.ItemsSource> </ListView> <ListBox x:Name="uiListBox" ItemsSource="{Binding Names}" /> </DockPanel> 

And then the filtering logic:

 public partial class Window1 : Window { public Window1() { InitializeComponent(); Names = new List<string>(); Names.Add("Robert"); Names.Add("Mike"); Names.Add("steve"); Names.Add("Jeff"); Names.Add("bob"); Names.Add("Dani"); this.DataContext = this; } public List<String> Names { get; set; } private void OnFilterClick(object sender, RoutedEventArgs e) { uiListView.Items.Filter = x => x.ToString()[0] == x.ToString().ToUpper()[0]; } } 
+2
source

This component

http://dotnetexplorer.blog.com/2011/04/07/wpf-itemscontrol-generic-staticreal-time-filter-custom-control-presentation/

able to filter two elements of WPF elements, both are tied to the same data source without any interference!

And it's full declarative XAML, not C # code!

Hope this helps!

0
source

All Articles