【问题标题】:How to make leftdouble click work on entire treeviewitem area not just on text?如何使左双击在整个 treeviewitem 区域上工作而不仅仅是在文本上?
【发布时间】:2019-08-22 13:30:41
【问题描述】:

我有一个 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>

在 UI 中看起来像这样:

现在,如果我双击命令触发的文本。当我双击灰色区域时,没有任何反应。我怎样才能实现这种行为?

【问题讨论】:

  • 应用ControlTemplate中的InputBindingTreeViewItem:stackoverflow.com/questions/2660760/…
  • 我在哪里可以获得默认控制模板?有没有类似于 style.basedon 属性的东西?
  • 您目前使用什么样式/模板?它看起来不像默认的。
  • Materialdesigninxamlthemes.
  • 请下次在您的问题中说明这一点。请参阅我的答案以解决您的问题。

标签: wpf xaml material-design-in-xaml


【解决方案1】:

定义一个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>

【讨论】:

  • 很遗憾还是不行,不过看起来好多了。
  • 究竟是什么不起作用?您是否设置了BorderBackground?实际上,您可以复制并粘贴我的示例。它适用于我,即当我在文本外部单击时触发该命令。
  • 好吧,这很奇怪,因为我几乎复制了你的代码。不幸的是,我的工作时间结束了。明天我再看一遍。
  • 不,不幸的是它不起作用。我也在一个单独的项目中对其进行了测试。我希望图片中的灰色区域可以点击
  • 我找到了一个“解决方案”,将treeviewitem的宽度属性设置为绑定祖先treeviewitem的ActualWidth,并将边框宽度绑定到祖先treeviewitem的宽度。问题是它的性能非常糟糕,所以我不能使用它
猜你喜欢
  • 2020-10-02
  • 1970-01-01
  • 2013-01-12
  • 2018-01-31
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
相关资源
最近更新 更多