简体   繁体   English

列表框是否保留每个ItemsSource的CollectionViewSource?

[英]Does ListBox keep the CollectionViewSource per ItemsSource?

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. 该控件看起来很像一个资源管理器窗口,其中“ CurrentItem”是打开的文件夹,某些“ ChildrenItems”是子文件夹或文件。

To display the ChildrenItems, I'm using a ListBox. 为了显示ChildrenItems,我使用了一个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). 当我单击一个子项时,我将新的CurrentItem设置为子项,并更新ChildrenItems(沿着树向下移动)。

So technically, I often modify the ItemsSource property of the ListBox. 因此,从技术上讲,我经常修改ListBox的ItemsSource属性。

I also have some standard commands like "Go Up", "Go Back", "Root", etc 我也有一些标准命令,例如“ Go Up”,“ Go Back”,“ Root”等

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. 我的“向上”命令遇到了一些问题,并注意到当我将列表框的ItemsSource属性更改为我之前设置的ItemsSource时,ListBox的SelectedItem重置为使用旧ItemsSource时选择的旧项目。

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. 我开始通过将“ CurrentItem”设置为Root来探索这种结构。 This mean that my ListBox ItemsSource is set to "Node1,Node2,Node3". 这意味着我的ListBox ItemsSource设置为“ Node1,Node2,Node3”。

I then click on the "Node1" (which changes the SelectedItem). 然后,我单击“ Node1”(这将更改SelectedItem)。 My Control then set its "CurrentItem" to this new SelectedItem, and also set the ListBox ItemsSource to "SubNode1a, SubNode1b". 然后,我的控件将其“ CurrentItem”设置为此新的SelectedItem,并将ListBox ItemsSource设置为“ 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. 此后,如果我使用“向上”命令,则将“ CurrentItem”重置为“ Root”,将ListBox ItemsSource重置为“ Node1,Node2,Node3”,但是有一个区别:选择了“ Node1”。

This makes me think of a CollectionViewSource which is reused, therefore keeping the selected item. 这使我想到了一个被重用的CollectionViewSource,因此保留了选定的项目。 Is this possible? 这可能吗?

Thanks for your insights. 感谢您的见解。

I found an interesting article from Bea Stollnitz where she says : 我从Bea Stollnitz找到了一篇有趣的文章 ,她说:

CollectionViewSource also enables another interesting scenario. CollectionViewSource还启用了另一个有趣的场景。 If a particular CollectionViewSource points to different collections at different times, it remembers all the views that it created to wrap those collections. 如果特定的CollectionViewSource在不同的时间指向不同的集合,则它会记住它创建的用于包装这些集合的所有视图。 If a source that has already been set in the past is set again, CVS recognizes it and reuses the view it created originally. 如果再次设置了过去已经设置的源,则CVS会识别它并重新使用它最初创建的视图。 This behavior is useful in hierarchical binding scenarios. 此行为在层次结构绑定方案中很有用。

That may be the answer I was looking for. 那可能就是我一直在寻找的答案。 My next question is then : Is it possible to disable this behavior? 我的下一个问题是:是否可以禁用此行为?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从ObservableCollection切换WPF ListBox ItemsSource <T> MVVM在运行时访问CollectionViewSource - Switching a WPF ListBox ItemsSource from ObservableCollection<T> to CollectionViewSource at runtime in MVVM 列表框不更新ItemsSource的内容 - Listbox does not update contents of ItemsSource 为什么ListBox不显示绑定的ItemsSource - Why ListBox does not display the bound ItemsSource 重新加载ItemsSource时如何在ListBox中保留选择 - How do I keep the selection in ListBox when reloading ItemsSource 使用CollectionViewSource重新填充列表框 - Repopulate Listbox Using a Collectionviewsource 将CollectionViewSource绑定到ListBox - Binding a CollectionViewSource to a ListBox wpf combobox itemssource collectionviewsource vs direct binding - wpf combobox itemssource collectionviewsource vs direct binding WPF ReactiveUI ListBox使用绑定的ItemsSource填充,但ComboBox不填充 - WPF ReactiveUI ListBox populates with bound ItemsSource but ComboBox does not 将Wpf HierarchicalDataTemplate ItemsSource绑定到字典中的CollectionViewSource? - Bind a Wpf HierarchicalDataTemplate ItemsSource to a CollectionViewSource in a dictionary? WPF ListBox ItemsSource 绑定未找到引用的路径 - WPF ListBox ItemsSource bindings does not find Path referenced
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM