简体   繁体   English

如何将命令绑定到TreeView内部项目的上下文菜单中?

[英]How to bind command into a context menu that is inside TreeView on it's items?

I want to be able to execute a command on an item inside the TreeView by clicking the command in the context menu. 我希望能够通过单击上下文菜单中的命令在TreeView内部的项目上执行命令。 And to be more specific only if the item is of a certain type (the xxTreeViewItem is an interface with 2 subtypes). 并且仅在项目是某种类型时才更具体(xxTreeViewItem是具有2个子类型的接口)。

   <Grid Name="Root" commonExtensions:EnterKeyUpdateExtension.IsEnabled="True">

    <StackPanel Orientation="Vertical" Grid.Row="0">
        <Button Content="Center on" Command="{Binding Path=CenterOnCommand}" Margin="5,10,5,0"/>
    </StackPanel>
        <Grid>
            <TreeView Grid.Row="0" Name="xxTreeView" DataContext="{Binding Path=xxViewModel}" ItemsSource="{Binding Path=Items}">
                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="ContextMenu">
                            <Setter.Value>
                                <ContextMenu>
                                    <MenuItem Header="Center On" Command="{Binding CenterOnCommand}"/>
                                </ContextMenu>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Path=Items}" DataType="{x:Type localViewItems:xxTreeViewItem}">
                        <TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>

The Command in the button on the top works, but it does not work below in the context menu. 顶部按钮中的“命令”有效,但上下文菜单下方的“命令”无效。 I tried several bindings and finding of the ancestor but none of it works. 我尝试了几次绑定并找到了祖先,但没有一个起作用。 Is there XAML only solution? 是否只有XAML解决方案?

The ContextMenu does not belong to the visual tree, so it does not inherit your TreeView's DataContext. ContextMenu不属于可视树,因此它不继承TreeView的DataContext。 So you need to pass it to your ContextMenu by using the PlacementTarget property: 因此,您需要使用PlacementTarget属性将其传递到ContextMenu:

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=TreeView}, Path=DataContext}"></Setter>
        <Setter Property="ContextMenu">
            <Setter.Value>
                <ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                    <MenuItem Header="Center On" Command="{Binding CenterOnCommand}" />
                </ContextMenu>
            </Setter.Value>
        </Setter>
    </Style>
</TreeView.ItemContainerStyle>

I hope it can help you 希望对您有所帮助

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

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