简体   繁体   中英

WPF - Change one ListBox's Items order affect the other ListBox

I have 2 ListBox which are bounded to the same source Photos (which is a collection of Photo)

I know that there is a way to get the CollectionView of the source (which is Photos) and then any change on this CollectionView (such as sorting for example) will affect on all Targets bounded to that source.

what I can't understand is why when i am changing the SortDescriptions of one ListBox, i see the change on the other also.

myListBox1.Items.SortDescriptions.Add(new SortDescription("ProperyName",ListDirection.Ascending));

makes both myListBox1 and myListBox2 to be changed..

any ideas? thanks!

From MSDN :

All collections have a default CollectionView. WPF always binds to a view rather than a collection. If you bind directly to a collection, WPF actually binds to the default view for that collection. This default view is shared by all bindings to the collection, which causes all direct bindings to the collection to share the sort, filter, group, and current item characteristics of the one default view. Alternatively, you can create a view of your collection in Extensible Application Markup Language (XAML) or code using the CollectionViewSource class, and binding your control to that view.

You could create a CollectionViewSource in XAML as two resources, and bind your ListBoxes to these views, instead of the collections directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM