从ContextMenu中的TreeViewItem中查找绑定项

时间:2010-08-24 20:35:34

标签: wpf treeview click binding contextmenu

我的树视图看起来像这样:

        <TreeView 
                Grid.Column="0"
                Grid.Row="2"
                MinHeight="100" 
                MinWidth="100"
                BorderBrush="White"
                DataContext="{Binding Projects, Source={x:Static SizingApp:Manager.Instance}}">
                <TreeView.Resources>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="IsExpanded" Value="True" />
                    </Style>
                    <DataTemplate x:Key="LoadTemplate">
                        <TextBlock Text="{Binding Name}">
                            <TextBlock.ContextMenu>
                                <ContextMenu>
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Edit_InputGestureText}">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Edit_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem>
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Save_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem>
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Export_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Copy_Header}"
                                                                    InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Copy_InputGestureText}"/>
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Remove_InputGestureText}">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Remove_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                </ContextMenu>
                            </TextBlock.ContextMenu>
                        </TextBlock>
                    </DataTemplate>
                    <HierarchicalDataTemplate x:Key="StepTemplate"
                            ItemsSource="{Binding Loads}" 
                            ItemTemplate="{StaticResource LoadTemplate}">
                        <TextBlock Text="{Binding Name}">
                            <TextBlock.ContextMenu>
                                <ContextMenu>
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Edit_InputGestureText}">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Edit_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Copy_Header}" 
                                                        InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Copy_InputGestureText}" />
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Paste_Header}" 
                                                        InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Paste_InputGestureText}" />
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Import_Load_Header}" />
                                    <MenuItem>
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Export_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Remove_InputGestureText}">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                                StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Remove_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=step_ContextMenu_Add_Load_Header}"
                                                        ItemsSource="{StaticResource mnuLoadSubmenuCommon}"/>
                                </ContextMenu>
                            </TextBlock.ContextMenu>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate x:Key="ProjectTemplate"
                            ItemsSource="{Binding Steps}"
                            ItemTemplate="{StaticResource StepTemplate}">
                        <TextBlock Text="{Binding Name}">
                            <TextBlock.ContextMenu>
                                <ContextMenu>
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Edit_InputGestureText}" 
                                              Click="EditProject_Click">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name, 
                                                    StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Edit_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem>
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name,
                                                    StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Save_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Copy_Header}" 
                                            InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Copy_InputGestureText}" />
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Paste_Header}" 
                                            InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Paste_InputGestureText}" />
                                    <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Remove_InputGestureText}">
                                        <MenuItem.Header>
                                            <TextBlock Text="{Binding Path=Name,
                                                    StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Remove_Header}}" />
                                        </MenuItem.Header>
                                    </MenuItem>
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Project_AddStep_Header}" />
                                    <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Project_ImportStep_Header}" />
                                </ContextMenu>
                            </TextBlock.ContextMenu>
                        </TextBlock>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
                <TreeViewItem 
                        Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Header}"
                        ItemsSource="{Binding}"
                        IsExpanded="True"
                        ItemTemplate="{StaticResource ProjectTemplate}">
                    <TreeViewItem.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Paste_Header}" 
                                    InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Paste_InputGestureText}" />
                        </ContextMenu>
                    </TreeViewItem.ContextMenu>
                </TreeViewItem>
            </TreeView>

这是我的点击事件

    private void EditProject_Click(object sender, RoutedEventArgs e)
    {
        //how do I find the bound item of the node this context menu resides on?
    }

我正在尝试为ContextMenu MenuItem点击事件编写EditProject_Click事件。当我单击它时,我希望能够找到绑定到上下文菜单所在节点的项目。我该怎么做?


答案

    private void EditProject_Click(object sender, RoutedEventArgs e)
    {
        Project project = (Project)((MenuItem)sender).DataContext;

        //Do stuff with the project here
    }

另外,如果你想让ContextMenu从ContextMenu附加的元素更新它的DataContext,这里的语法是:

<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">

1 个答案:

答案 0 :(得分:6)

在点击事件处理程序中,“(发件人为FrameworkElement).DataContext”会是您要找的吗?

修改

以下示例代码。

XAML:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:v="clr-namespace:WpfApplication3"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="template">
            <TextBlock Text="{Binding}">
                <TextBlock.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Test" Click="MenuItem_Click"/>
                    </ContextMenu>
                </TextBlock.ContextMenu>
            </TextBlock>
        </DataTemplate>
    </Window.Resources>
    <DockPanel>
        <Button DockPanel.Dock="Top" Click="Button_Click">Change Context</Button>

        <TreeView x:Name="tv" ItemTemplate="{StaticResource template}"/>

    </DockPanel>

</Window>

代码隐藏:


public partial class MainWindow : Window
{
    bool flag = false;
    public MainWindow()
    {
        InitializeComponent();
        List list = new List() { "A", "B", "C", "D" };
        this.tv.ItemsSource = list;
    }
    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show((sender as MenuItem).DataContext.ToString());
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (flag)
        {
            List list = new List() { "A", "B", "C", "D" };
            this.tv.ItemsSource = list;
        }
        else
        {
            List list = new List() { 1, 2, 3, 4 };
            this.tv.ItemsSource = list;
        }
        flag = !flag;
    }

}