How can I filter the ItemSources of two ListBoxes that have a common CollectionViewSource

Hi,
I have a WPF MainWindow that has a CollectionViewSource in the Window.Resources section.
In the main layout grid I have two ListBox controls.
I would like to use a filtered version of the CollectionViewSource for each listbox, so that they each show different ListItems from the common source.
Is there any way I can do this?
Preferrably by using a property (or attached property) in xaml on each ListBox as a way of determining the filter.
 
quentinAAsked:
Who is Participating?
 
politexCommented:
Hi, you can make like this:
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" >
        <Grid>
        <ListBox x:Name="lb1"  HorizontalAlignment="Left" Width="150" DisplayMemberPath="Name" />
        <ListBox x:Name="lb2" HorizontalAlignment="Right" Width="150" DisplayMemberPath="Name" />
        </Grid>
</Window>

Open in new window

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CollectionViewSource cvs = new CollectionViewSource();
            cvs.Source = AsterixCharacters;

            CollectionViewSource cvs2 = new CollectionViewSource();
            cvs2.Source = AsterixCharacters;

            Predicate<object> hWhite = (object p) => { return ((AsterixCharacter)p).Hair == HairColor.White; };
            cvs.View.Filter = hWhite;
            lb1.ItemsSource = cvs.View;

            Predicate<object> hBlond = (object p) => { return ((AsterixCharacter)p).Hair == HairColor.Blond; };
            cvs2.View.Filter = hBlond;
            lb2.ItemsSource = cvs2.View;
        }

Open in new window

0
 
Bob LearnedCommented:
I don't quite understand what you are working with, and what you are asking for...
0
 
quentinAAuthor Commented:
I have a list of Asterix the Gaul characters with their hair color:

public MainWindowViewModel()
        {
            this.AsterixCharacters = new ObservableCollection<AsterixCharacter>();

            this.AsterixCharacters.Add(new AsterixCharacter("Asterix", HairColor.Blond));
            this.AsterixCharacters.Add(new AsterixCharacter("Assurancetourix", HairColor.Blond));
            this.AsterixCharacters.Add(new AsterixCharacter("Maestria", HairColor.Blond));
            this.AsterixCharacters.Add(new AsterixCharacter("Idefix", HairColor.White));
            this.AsterixCharacters.Add(new AsterixCharacter("Goudurix", HairColor.Blond));
            this.AsterixCharacters.Add(new AsterixCharacter("Mme Agecanonix", HairColor.Red));
            this.AsterixCharacters.Add(new AsterixCharacter("Gueuselambix", HairColor.Blond));
            this.AsterixCharacters.Add(new AsterixCharacter("Panoramix", HairColor.White));
            this.AsterixCharacters.Add(new AsterixCharacter("Pepe", HairColor.Black));
            this.AsterixCharacters.Add(new AsterixCharacter("Obelix", HairColor.Red));
            this.AsterixCharacters.Add(new AsterixCharacter("Abraracourcix", HairColor.Red));
        }


        public ObservableCollection<AsterixCharacter> AsterixCharacters;

.....................................................
the MainWindow is set up to use the collection above for its <CollectionViewSource>

 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            asterixCharacters = new ObservableCollection<AsterixCharacter>(new MainWindowViewModel().AsterixCharacters);          
            cvs = (CollectionViewSource)(this.FindResource("cvs"));
            cvs.Source = asterixCharacters;
            view = (ListCollectionView)CollectionViewSource.GetDefaultView(cvs);
         }

................................................
the MainWindow has 2 listboxes:
 
 <Window.Resources>
         <CollectionViewSource x:Key="cvs"/>
 </Window.Resources>


<Grid>
      <ListBox />
      <ListBox/>
 </Grid>
.....................................................

I would like to have a property on each ListBox, that can be assigned in XAML, that causes one listbox to show those Asterix characters with blond hair; and the second listbox to show those characters with red hair.
Ultimately, i'm trying to understand this in order to make a custom control out of a listbox a few buttons and a combobox.
............................................................

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.