简体   繁体   中英

How to Set DataContext for TreeViewItem for ContextMenu

I have the following code. I am trying to bind the Command property of the MenuItem to the relevant command which is implemented in the DataContext of the user control. Can anyone help me? The command does not get executed.

<UserControl x:Class="MIB2.App.Presentations.Views.CategoryManView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:model="clr-namespace:MIB2.Models;assembly=MIB2"
             xmlns:local="clr-namespace:MIB2.App.Presentations.Views"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="335">
    <UserControl.Resources>
        <local:DatabindingDebugConverter x:Key="debugConverter" />
    </UserControl.Resources>

    <StackPanel Width="417">
        <TreeView   x:Name="tree"
                    ItemsSource="{Binding RootCategories}" SelectedItemChanged="TreeView_SelectedItemChanged"
                    MouseRightButtonDown="OnPreviewMouseRightButtonDown">
            <TreeView.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Add Root Category" Command="{Binding AddRootCategoryCommand}"/>
                </ContextMenu>
            </TreeView.ContextMenu>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate   DataType="model:Item"
                                            ItemsSource="{Binding Children}">
                    <Grid>
                        <Grid.ContextMenu>
                            <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=TextBox}}">
                                <MenuItem Header="Add Category" Command="{Binding Path=AddEntityCommand}" />
                            </ContextMenu>
                        </Grid.ContextMenu>

                        <TextBlock Text="{Binding Description}" Margin="0,0,10,0" Tag="{Binding DataContext, ElementName=tree}"/>
                    </Grid>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        <StackPanel Orientation="Horizontal">
            <Button Content="Add New Category" Command="{Binding AddEntityCommand}" />
            <Button Content="Add New Root Category" Command="{Binding AddRootCategoryCommand}" />
            <Button Content="Delete Category" Command="{Binding DeleteEntityCommand}" />
        </StackPanel>
        <ContentControl Content="{Binding EntityViewModel.View}" />
    </StackPanel>
</UserControl>

Please try this:

<TreeView.ItemTemplate>
    <HierarchicalDataTemplate DataType="model:Item"
                              ItemsSource="{Binding Children}">
        <Grid Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">
            <Grid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Add Category"
                              Command="{Binding Path=PlacementTarget.Tag.AddEntityCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
                </ContextMenu>
            </Grid.ContextMenu>
            <TextBlock Text="{Binding Description}" Margin="0,0,10,0" />
        </Grid>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

您可以在绑定中使用RelativeSource来查找父对象,例如

<MenuItem Header="Add Root Category" Command="{Binding DataContext.CommandName, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}/> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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