I have a TreeView and there are multiple layers to the TreeView using HierarchicalDataTemplates. Each TreeViewItem currently displays the "name" of the object that it represents. I now need to add CheckBoxes to the children of the tree which I have figured out somewhat, but I haven't figured out to access the NAME of the checkbox when it is checked.
我有一个TreeView,使用HierarchicalDataTemplates有多个TreeView层。每个TreeViewItem当前显示它所代表的对象的“名称”。我现在需要将CheckBoxes添加到我已经想到的树的子项中,但是在检查时我还没想过要访问该复选框的NAME。
To be more specific, I need the name of the checkbox that is checked because I need to apply a filter to some data accordingly. How do I access the name of each CheckBox when it is checked if the CheckBox and Name of the object for the TreeViewItem are created dynamically from data?
更具体地说,我需要选中复选框的名称,因为我需要相应地对某些数据应用过滤器。当检查TreeViewItem对象的CheckBox和Name是否是从数据动态创建时,如何访问每个CheckBox的名称?
1 个解决方案
#1
1
i'm assuming you have a corresponding command in your viewmodel which all the checkbox items in the tree are bound to.
我假设你的viewmodel中有一个相应的命令,树中的所有复选框项都绑定到该命令。
bind to a command and send to current name of the ComboBoxItem as a CommandParameter
绑定到命令并作为CommandParameter发送到ComboBoxItem的当前名称
<DataTemplate x:Key="ListBoxItemTemplate" DataType="{x:Type ListBoxItem}">
<CheckBox Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox},
Path=DataContext.CheckBoxItemCheckedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Name}"/>
</DataTemplate>
#1
1
i'm assuming you have a corresponding command in your viewmodel which all the checkbox items in the tree are bound to.
我假设你的viewmodel中有一个相应的命令,树中的所有复选框项都绑定到该命令。
bind to a command and send to current name of the ComboBoxItem as a CommandParameter
绑定到命令并作为CommandParameter发送到ComboBoxItem的当前名称
<DataTemplate x:Key="ListBoxItemTemplate" DataType="{x:Type ListBoxItem}">
<CheckBox Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox},
Path=DataContext.CheckBoxItemCheckedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Name}"/>
</DataTemplate>