2013-02-15 127 views
1

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

<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> 

这是行不通的。如果我在不在树视图中的按钮上使用我的命令,那么命令会被触发。我怎么解决这个问题?

回答

2

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

使用AncestorType来指代的TreeView的DataContext(这是相同的Windows DC,如果你没有设置manuelly):

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

哇。这样可行。 :-) – user2025830 2013-02-15 12:37:19