1

I'm writing a control to explore a tree structure which can be dynamically created. This controls looks a lot like an explorer window, with a "CurrentItem" being the opened folder, some "ChildrenItems" being subfolders or files.

To display the ChildrenItems, I'm using a ListBox. When I click on a Child Item, I set the new CurrentItem to be the child Item, and update the ChildrenItems (going down the tree).

So technically, I often modify the ItemsSource property of the ListBox.

I also have some standard commands like "Go Up", "Go Back", "Root", etc

I have had some problem with the "Go Up" command, and noticed that when I change the ItemsSource property of the listbox to an ItemsSource I previoulsy set, the SelectedItem of the ListBox is reset to the old item selected when the old ItemsSource was used.

Maybe a little exemple would be more explicit : Let's say I have this tree structure :

 - Root
   - Node 1
     - SubNode 1a
     - SubNode 1b
   - Node 2
   - Node 3

I begin exploring this structure by setting the "CurrentItem" as the Root. This mean that my ListBox ItemsSource is set to "Node1,Node2,Node3".

I then click on the "Node1" (which changes the SelectedItem). My Control then set its "CurrentItem" to this new SelectedItem, and also set the ListBox ItemsSource to "SubNode1a, SubNode1b".

After that, if I use my "Go Up" command, the "CurrentItem" is reset to "Root", and the ListBox ItemsSource to "Node1,Node2,Node3", but there is a difference : The "Node1" is selected.

This makes me think of a CollectionViewSource which is reused, therefore keeping the selected item. Is this possible?

Thanks for your insights.

4

1 回答 1

1

我发现了Bea Stollnitz的一篇有趣的文章,她说:

CollectionViewSource 还启用了另一个有趣的场景。如果一个特定的 CollectionViewSource 在不同的时间指向不同的集合,它会记住它为包装这些集合而创建的所有视图。如果再次设置了过去已设置的源,CVS 会识别它并重用它最初创建的视图。此行为在分层绑定方案中很有用。

这可能就是我一直在寻找的答案。我的下一个问题是:是否可以禁用此行为?

于 2012-05-01T15:14:20.877 回答