簡體   English   中英

如何為ContextMenu設置TreeViewItem的DataContext

[英]How to Set DataContext for TreeViewItem for ContextMenu

我有以下代碼。 我試圖將MenuItemCommand屬性綁定到在用戶控件的DataContext中實現的相關命令。 誰能幫我? 該命令未執行。

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

請試試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM