WPF 在单击鼠标时展开 TreeView [英] WPF expand TreeView on single mouse click
问题描述
我有一个带有 HierarchicalDataTemplate 的 WPF TreeView.
I have a WPF TreeView with a HierarchicalDataTemplate.
目前我必须双击一个项目才能展开/折叠它.
Currently I have to double click an item to expand/collapse it.
我想将此行为更改为单击一次,而不会丢失其他功能.所以它应该在点击时展开和折叠.
I would like to change this behaviour to a single click, without loosing other functionality. So it should expand and collapse on click.
推荐的方法是什么?
谢谢!
推荐答案
您可以使用重新模板化的复选框作为您的节点(包含您当前使用的任何模板),并将其 IsChecked 属性绑定到 TreeViewItem 的 IsExpanded 属性.
You could use a re-templated checkbox as your node (containing whatever template you are currently using) with its IsChecked property bound to the IsExpanded property of the TreeViewItem.
这是我刚刚测试过的一个模板,它似乎可以完成这项工作:
Here is a template I've just test that seems to do the job:
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}">
<CheckBox.Template>
<ControlTemplate>
<TextBlock Text="{Binding Header}"></TextBlock>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>
</HierarchicalDataTemplate>
只需用您需要的任何内容替换 ControlTemplate 内容即可.
Just replace the ControlTemplate contents with whatever you need.
这篇关于WPF 在单击鼠标时展开 TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!