自定义 TreeView 以允许多选
内置的 WPF TreeView 控件不允许像 ListBox 那样进行多项选择。 如何自定义 TreeView 以允许多重选择而不重写它。
The built-in WPF TreeView control does not allow for multi selection, like a ListBox does. How can I customize the TreeView to allow for multi selection without rewriting it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我有一个 SoMoS 实现的变体,它使用在基本 TreeView 控件的派生中声明的附加属性来跟踪 TreeViewItems 的选择状态。 这可以保持对 TreeViewItem 元素本身的选择跟踪,而不是对树视图呈现的模型对象进行跟踪。
这是新的 TreeView 类派生。
这是 XAML。 请注意,最重要的部分是使用 MultiSelectTreeViewItemStyle 中新的“IsItemSelected”附加属性来替换使用单一“IsSelected”属性的两个触发器,以实现视觉状态。
另请注意,我没有将新的 TreeView 控件聚合到 UserControl 中。
这是一个驱动它的俗气视图模型(用于演示目的)。
这是 MainWindow 代码隐藏中的按钮单击处理程序,用于显示 MessageBox 中的选择。
希望这可以帮助。
I have a variation on SoMoS implementation that uses an attached property declared on a derivation of the base TreeView control to track the selection state of the TreeViewItems. This keeps the selection tracking on the TreeViewItem element itself, and off of the model object being presented by the tree-view.
This is the new TreeView class derivation.
And here's the XAML. Make note that the salient part is the replacement of the two triggers that use the singular 'IsSelected' property with the new 'IsItemSelected' attached property in the MultiSelectTreeViewItemStyle to achieve the visual state.
Also note I'm not aggregating the new TreeView control into a UserControl.
And here's a cheesy view-model to drive it (for demo purposes).
And here's the button click-handler on the MainWindow code-behind that shows the selections in a MessageBox.
Hope this helps.
当我考虑重写控件(如树视图)的基本行为时,我总是喜欢考虑与我的决定相关的可用性和工作量。
在树视图的具体情况下,我发现切换到列表视图并结合零个、一个或多个控件可以形成更可用的解决方案,而且通常更容易实现。
作为示例,请考虑常见的“打开”对话框或 Windows 资源管理器应用程序。
When I consider overriding the fundamental behavior of a control, like a treeview, I always like to consider the usability and effort associated with my decision.
In the specific case of a treeview I find that switching to a listview in combination with zero, one, or more controls makes for a more usable solution that often is easier to implement.
As an example, consider the common Open dialog, or Windows Explorer application.
我简化了此任务,在每个树视图项的文本前添加一个复选框。
因此,我创建了一个包含 2 个项目的停靠面板:复选框 + 文本块。
所以...
XAML
CS
然后您可以访问复选框值:
如果您不需要任何复杂的内容,这是一种简单的方法。
I've simplified this task adding a checkbox before the text for each treeviewitem.
So, I've created a dockpanel with 2 items inside: checkbox + textblock.
So...
XAML
CS
And then you can access checkbox value:
This is a simple way to do if you don't need anything complex.
我终于结束了自己的自定义控件的编码,其中包含一个 TreeView 。 基于其他人的工作,功能的关键在于使 TreeView 的模型的所有项都继承接口 ISelectable:
这样我们将拥有一个新的“IsSelected”属性,该属性与 TreeViewItem IsSelected 无关。 我们只需要设置树的样式,以便它处理模型的 IsSelected 属性。 这里是代码(它使用 http://code.html 上提供的拖放库。 google.com/p/gong-wpf-dragdrop/):
XAML
C#:
I finally ended coding my own CustomControl containing a TreeView inside. Based on the work of others the key of the functionality resides on making all the items of the Model of the TreeView inherit the interface ISelectable:
This way we will have a new 'IsSelected' property that has nothing to do with the TreeViewItem IsSelected. We just need to style our tree so it handles the model IsSelected property. Here the code (it's using the Drag & drop libraries available at http://code.google.com/p/gong-wpf-dragdrop/):
XAML
C#:
@Nishan Hossepian 答案很好,但最好重写
SelectedItemChangedInternal
方法,如下所示:@Nishan Hossepian answer is good, but it is better to rewrite the
SelectedItemChangedInternal
method like this:我使用 PropertyTools.WPF 包中的多选 TreeListBox 控件。
效果很好。 Examples 文件夹中有一个 TreeListBox 的示例。
github 链接
发布以防有人仍在寻找此问题的答案。
I use the multi-select TreeListBox control from the PropertyTools.WPF package.
Works great. There is an example of TreeListBox in the Examples folder.
Link to github
Posting in case someone is still looking for an answer to this question.