TreeView的contextmenu出现在topnode而不是TreeViewItem上

时间:2013-12-11 08:07:12

标签: c# wpf treeview

我遇到了一个我无法找到解决方案的问题。这是我的TreeView的XAML:

    <TreeView ItemsSource="{Binding Parents}" ContextMenu="{StaticResource TreeViewContextMenu}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Image}" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

我在ContentControl.Resources

中也有这种风格
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
        <Setter Property="ContextMenu" Value="{StaticResource TreeViewItemContextMenu}" />
    </Style>

当我右键单击除第一个项目之外的任何项目时,TreeViewItemContextMenu出现,就像预期一样。 但问题是,当我右键单击topnode时,会出现TreeViewContextMenu。 我试着没有TreeViewContextMenu,但是topnode也没有ContextMenu

我很高兴知道,我错过了什么和哪里。提前谢谢。

1 个答案:

答案 0 :(得分:1)

我正在尝试使用您的xaml code。这是有效的。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ContextMenu x:Key="TreeViewItemContextMenu">
            <MenuItem Header="Item Menu"/>
        </ContextMenu>
        <ContextMenu x:Key="TreeViewContextMenu">
            <MenuItem Header="Tree Menu"/>
        </ContextMenu>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
            <Setter Property="ContextMenu" Value="{StaticResource TreeViewItemContextMenu}" />
        </Style>
    </Window.Resources>
    <Grid>
        <TreeView ItemsSource="{Binding Parents}" ContextMenu="{StaticResource TreeViewContextMenu}">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding Image}" />
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

代码

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Parents = new ObservableCollection<MyTreeItem>();
            var children=new ObservableCollection<MyTreeItem>();
            children.Add(new MyTreeItem(){ Name="child1"});
            children.Add(new MyTreeItem(){ Name="child2"});
            Parents.Add(new MyTreeItem() { Name = "Parent Node", Children = children });

            this.DataContext = this;
        }

        public ObservableCollection<MyTreeItem> Parents { get; set; }

    }

    public class MyTreeItem {
        public string Name { get; set; }
        public ObservableCollection<MyTreeItem> Children { get; set; }
    }

当我点击顶部节点和子节点时,上下文菜单是相同的

enter image description here