1

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>

これは機能しません。ツリービューにないボタンでコマンドを使用すると、コマンドが実行されます。どうすればこの問題を解決できますか?

4

2 に答える 2

2

このコマンドがTreeViewの外部で機能する場合、TreeViewClickCommandはWindow/UserControlのDataContextにあると思います。

AncestorTypeを使用して、TreeViews DataContextを参照します(手動で設定しなかった場合は、Windows DCと同じです)。

Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, Path=DataContext.TreeViewClickCommand}" 
于 2013-02-15T12:33:44.397 に答える
-1

これを行うには、 AttachedCommandBehaviorを使用できます。

于 2013-02-15T12:38:35.360 に答える