[英]How to make leftdouble click work on entire treeviewitem area not just on text?
I have a TreeView and i use an ItemTemplate for the Item Bindings similar to this: 我有一个TreeView,我使用ItemTemplate来进行类似于以下的项目绑定:
<TreeView ItemsSource="{Binding TreeViewItemCollection}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<StackPanel.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding Command}" CommandParameter="{Binding CommandParameter}"></MouseBinding>
</StackPanel.InputBindings>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
In the Ui it looks something like this : 在Ui中,它看起来像这样:
Now if i doubleclick on the text the command triggers. 现在,如果我双击文本,命令将触发。 When i doubleclick on the grey area nothing happens.
当我双击灰色区域时,没有任何反应。 How can i achieve this behaviour?
我该如何实现这种行为?
Define an ItemContainerStyle
that stretches the content and removes the default padding, and then put the StackPanel
in an element that adds the padding back: 定义一个
ItemContainerStyle
来拉伸内容并删除默认的填充,然后将StackPanel
放在添加填充的元素中:
<TreeView ItemsSource="{Binding TreeViewItemCollection}">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem" BasedOn="{StaticResource MaterialDesignTreeViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="Padding" Value="0" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<Border Background="Transparent" Padding="8">
<Border.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding Command}" CommandParameter="{Binding CommandParameter}" />
</Border.InputBindings>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></TextBlock>
</StackPanel>
</Border>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.