【问题标题】:Mousebinding in a WPF TreeViewWPF TreeView 中的鼠标绑定
【发布时间】:2013-02-15 11:40:19
【问题描述】:

有没有办法在 WPF 树视图中执行鼠标或键绑定?

<TreeView ItemsSource="{Binding Main.TreeItems}">
    <TreeView.ItemTemplate>                           
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">                          
            <TextBlock Text="{Binding Path=Header}">
                <TextBlock.InputBindings>
                    <MouseBinding Command="{Binding TreeViewClickCommand}" MouseAction="LeftDoubleClick"/>
                </TextBlock.InputBindings>
             </TextBlock>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

这不起作用。如果我在不在树视图中的按钮上使用我的命令,则该命令被触发。我该如何解决这个问题?

【问题讨论】:

    标签: wpf xaml treeview mouse key-bindings


    【解决方案1】:

    如果此命令在 TreeView 之外工作,我假设您的 TreeViewClickCommand 位于您的 Window/UserControl 的 DataContext 中。

    使用 AncestorType 来引用 TreeViews DataContext(与 Windows DC 相同,如果你没有手动设置的话):

    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, Path=DataContext.TreeViewClickCommand}" 
    

    【讨论】:

      【解决方案2】:

      您可以使用AttachedCommandBehavior 来执行此操作。

      【讨论】:

      • 想评论一下为什么这不适用?
      猜你喜欢
      • 2011-04-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多