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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.