Link to home
Start Free TrialLog in
Avatar of quentinA
quentinAFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
 
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I don't quite understand what you are working with, and what you are asking for...
Avatar of quentinA

ASKER

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.
............................................................

ASKER CERTIFIED SOLUTION
Avatar of politex
politex

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial