开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜