简体   繁体   English

树视图上下文菜单-将所选项目传递给命令?

[英]Tree View Context Menu - Pass selected item to command?

I'm wondering how to pass the selected item to a command from a treeview / HierarchicalDataTemplate ? 我想知道如何将所选项目从treeview / HierarchicalDataTemplate传递给命令?

Here is the code that I have so far, it displays the context menu but I haven't bound the commands to it yet. 这是我到目前为止的代码,它显示了上下文菜单,但是我尚未将命令绑定到该菜单。 The command binding is the easy part, but how do I tell which node it came from ? 命令绑定是简单的部分,但是如何知道它来自哪个节点呢?

<HierarchicalDataTemplate 
    DataType="{x:Type viewModel:UsersViewModel}" 
    ItemsSource="{Binding Children}">
    <StackPanel Orientation="Horizontal">
        <Image Width="16" Height="16" Margin="3,0" Source="Images\Region.png" />
        <TextBlock Text="{Binding UserName}">
            <TextBlock.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Edit" />
                        <MenuItem Header="Delete"/>
                     </ContextMenu>
                </TextBlock.ContextMenu>
        </TextBlock>
    </StackPanel>
</HierarchicalDataTemplate>

Just {Binding} should be the whole item. {Binding}应该是整个项目。

(To pass it to the Command bind the CommandParameter , in Execute and CanExecute it will become the method parameter (which you then need to cast to your item-type)) (将其传递到Command绑定CommandParameter ,在ExecuteCanExecute它将成为方法的参数(你需要再投放到项目型))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM