简体   繁体   English

如何使左键双击在整个treeviewitem区域而不只是文本上起作用?

[英]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中,它看起来像这样: TreeView用户界面

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.

相关问题 如何在TreeviewItem中制作文字换行? - How to make Text Wrap in TreeviewItem? 如何在WPF TreeViewItem中进行文本换行? - How to make text wrap in a WPF TreeViewItem? TreeViewItem:如何在Click而不是MouseDown上进行选择更改? - TreeViewItem: How to make selection change occur on Click instead of MouseDown? 如何使TreeViewItem不可折叠 - How to make TreeViewItem non collapsible 如何使现有TreeView的TreeViewItem引发Click事件? - How do I make the TreeViewItem(s) of an existing TreeView raise a Click event? 为什么我的TreeViewItem文本没有包含在示例代码中? 如何包装? - Why my TreeViewItem text do not wrap in my sample code? How to make it wrap? 如何使多选组合框的整个项目区域都可单击 - How to make the entire item area of multi-select combobox clickable WPF TreeView如何为treeViewItem的IsSelected触发 - WPF TreeView How to make trigger for IsSelected of treeViewItem 如何使用IValueConverter隐式生成的TreeViewItem折叠? - How to make implicitly generated TreeViewItem collapse with IValueConverter? WPF TreeView:如何使控件相对于每个 TreeViewItem 对齐,但仍然获得 header 文本的缩进效果? - WPF TreeView: How to make controls align relative to every TreeViewItem, yet still get the indentation effect for the header text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM