blue18hutthutt
blue18hutthutt

Reputation: 3243

Can differently grouped ICollectionViews still be synchronized between controls?

Suppose I have an in-memory collection of sports players including the college they attended, position they play, and current team, bound to 3 different controls (ListBoxes) in order to browse different players by those 3 different groupings

Is it possible to synchronize the 3 controls with different ICollectionViews so that a selection in one is synchronized with the others? Or is this only possible if they are all bound to the same ICollectionView instance? I have the 3 ListBoxes already set IsSynchronizedWithCurrentItem="true" but I suspect this only works if they're using the same ICollectionView?

This is what I'm doing in my code:

        ICollectionView collegeView = new ListCollectionView(playerDatabase);
        collegeView.GroupDescriptions.Add(new PropertyGroupDescription("CollegeName"));
        collegeView.SortDescriptions.Add(new SortDescription("CollegeName", ListSortDirection.Ascending));
        collegeView.SortDescriptions.Add(new SortDescription("FirstName", ListSortDirection.Ascending));
        lstCollege.ItemsSource = collegeView;

        ICollectionView positionView = new ListCollectionView(playerDatabase);
        positionView.GroupDescriptions.Add(new PropertyGroupDescription("PositionCode", new NFLPositionGrouper()));
        positionView.SortDescriptions.Add(new SortDescription("PositionName", ListSortDirection.Ascending));
        positionView.SortDescriptions.Add(new SortDescription("FirstName", ListSortDirection.Ascending));

The playerDatabase variable is coming from a LINQ to Entity query projecting an anonymous type.

BTW there are approximately 2000 rows in the original database - it seems like the initial interface takes a noticeable 1 second to draw itself initially is there any other way I can do this more efficiently? As long as I don't replace the ControlTemplate and thus remove the VirtualizingStackPanel (I do however have DataTemplates for the ItemTemplate and HeaderTemplate) I won't unintentionally disable item virtualization will I?

Upvotes: 0

Views: 61

Answers (1)

Brian S
Brian S

Reputation: 5785

The CollectionView acts much like a View in a database in that it is kind of like a live snapshot of the actual data which you can group, sort, filter, etc. If you are using 3 separate CollectionViews, I don't believe IsSynchronizedWithCurrentItem will work in this case because I believe that may depend on a single, ICollectionView.

However there is no reason you couldn't create a property in your ViewModel (are you using MVVM? If not, I'd highly recommend it) which represents the CurrentPlayer and bind the SelectedItem property of all the ListBoxes to this property (make sure the property raises the NotifyPropertyChanged event).

Upvotes: 1

Related Questions