如何在WPF中使用复选框开发TreeView? [英] How to develop treeview with checkboxes in wpf?
问题描述
我有一个要求,我需要将节点动态添加到 TreeView
,并且该节点必须具有 CheckBox
es 。如果选择了一个 CheckBox
,也选择了孩子。
I have a requirement that , I need to add nodes to a TreeView
dynamically and that nodes with CheckBox
es. If one CheckBox
is selected childs also selected.
主要是我想向 TreeView
动态。
推荐答案
一旦您知道如何做,这非常简单。
This is remarkably straightforward to do, once you know how.
为树形视图项目数据创建一个视图模型类(在这里我将其称为 CheckableItem
)。它需要这三件事:
Create a view model class (I've called it CheckableItem
here) for your tree view item data. It needs these three things:
- 它必须实现INotifyPropertyChanged。
- 它需要一个<$ c
ObservableCollection< CheckableItem> 类型的$ c> Children 属性。 - 它需要
IsChecked
属性类型Visibility
的属性,该属性在其设置器中引发了PropertyChanged
,并且遍历儿童
中的项目并设置其IsChecked
属性。
- It must implement INotifyPropertyChanged.
- It needs a
Children
property of typeObservableCollection<CheckableItem>
. - It needs an
IsChecked
property of typeVisibility
that, in its setter, raisesPropertyChanged
and also iterates through the items inChildren
and sets theirIsChecked
property.
在此类中实施其他属性以将项目的数据公开以进行绑定(我的示例仅假设称为 Value
的东西)。或者,您可以只实现类型为 object
的 Item
类,并使用 ContentPresenter
放在模板中,但我将留给您弄清楚。
Implement other properties in this class to expose the items' data to binding (my example just assumes something called Value
). Or you can just implement an Item
class of type object
and use a ContentPresenter
in the template, but I'll leave figuring that out to you.
现在创建一个 HierarchicalDataTemplate
对于您的类,如下所示:
Now create a HierarchicalDataTemplate
for your class that looks something like this:
<HierarchicalDataTemplate
DataType="{x:Type local:CheckableItem}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}"/>
<TextBlock Text="{Binding Value}"/>
</StackPanel>
</HierarchicalDataTemplate>
...以及 TreeView
它(当然,我假设您已经填充了这些对象的集合):
...and a TreeView
that uses it (I'm assuming you've populated a collection of these objects, of course):
<TreeView ItemsSource="{Binding MyCollectionOfCheckableItems}"/>
工作原理: TreeView
使用 HierarchicalDataTemplate
呈现其 ItemsSource
中的每个项目。 HierarchicalDataTemplate
是创建 HeaderedItemsControl
(在本例中为 TreeViewItem
),使用其模板呈现标头,然后将其 ItemsSource
用作控件项的来源-因为它们都是 CheckableItem
s由 HierarchicalDataTemplate
转换为 TreeViewItem
s。在那之后,它一直是乌龟。
How it works: The TreeView
uses the HierarchicalDataTemplate
to render each item in its ItemsSource
. The HierarchicalDataTemplate
is a template that creates a HeaderedItemsControl
(in this case a TreeViewItem
), uses its template to render the header, and then uses its ItemsSource
as the source for the control's items - which, since they're all CheckableItem
s, are turned into TreeViewItem
s by the HierarchicalDataTemplate
. After that, it's turtles all the way down.
这很好地概述了 TreeView
在实际中的工作原理,尽管正如我发现的大多数示例一样,它有很多麻烦吹口哨的是,很难看出基本原理的简单性。如果您了解MVVM,则上一段内容是您需要了解的内容的90%。
This is a pretty good overview of how TreeView
actually works in practice, though as with most examples I've found, it's got so many bells and whistles that it's sort of hard to see how simple the underlying principles are. If you understand MVVM, the previous paragraph is 90% of what you need to know.
这篇关于如何在WPF中使用复选框开发TreeView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!