Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF:防止上下文菜单出现在子树视图项上_Wpf_Data Binding_Treeview_Contextmenu - Fatal编程技术网

WPF:防止上下文菜单出现在子树视图项上

WPF:防止上下文菜单出现在子树视图项上,wpf,data-binding,treeview,contextmenu,Wpf,Data Binding,Treeview,Contextmenu,我试图设计一个WPF树视图,其中ContextMenu在特定节点上激活 在我的示例中,尽管我尽了最大的努力,但当单击BarNode的子节点FooNode时,我无法阻止它的ContextMenu出现 C#: public abstract class NodeBase { public NodeBase[] ChildNodes { get; set; } } public class FooNode : NodeBase { } public class BarNode : Nod

我试图设计一个WPF树视图,其中ContextMenu在特定节点上激活

在我的示例中,尽管我尽了最大的努力,但当单击BarNode的子节点FooNode时,我无法阻止它的ContextMenu出现

C#:

public abstract class NodeBase
{
    public NodeBase[] ChildNodes { get; set; }
}

public class FooNode : NodeBase
{

}

public class BarNode : NodeBase
{
}

public class ExampleModel : BaseModel
{
    private NodeBase[] _nodes;

    public NodeBase[] Nodes
    {
        get
        {
            _nodes = new NodeBase[]
                {
                    new FooNode(), 
                    new BarNode()
                        {
                            ChildNodes = new NodeBase[]
                            {
                                new FooNode(),
                                new FooNode()
                            }
                        }
                };
            return _nodes;
        }
    }

    public ExampleModel()
    {

    }
}

public class TreeViewStyleSelector : StyleSelector
{
    public Style FooNodeStyle { get; set; }

    public Style BarNodeStyle { get; set; }

    public override Style SelectStyle(object item, DependencyObject container)
    {
        var fooNode = item as FooNode;
        if (fooNode != null)
        {
            return FooNodeStyle;
        }

        var barNode = item as BarNode;
        if (barNode != null)
        {
            return BarNodeStyle;
        }

        return base.SelectStyle(item, container);
    }
}
XAML

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Nodes="clr-namespace:UnderstandingWPFTreeView.Nodes"
        xmlns:Models="clr-namespace:UnderstandingWPFTreeView.Models"
        xmlns:Common="clr-namespace:UnderstandingWPFTreeView.Common" 
    x:Class="UnderstandingWPFTreeView.MainWindow" 
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <Models:ExampleModel/>
    </Window.DataContext>

    <Window.Resources>
        <ContextMenu x:Key="testContextMenu">
            <MenuItem Header="Test Context Item"></MenuItem>
            <MenuItem Header="Test Context Item"></MenuItem>
        </ContextMenu>

        <Style TargetType="{x:Type TreeViewItem}"  x:Key="FooNodeStyle">
        </Style>

        <Style TargetType="{x:Type TreeViewItem}"  x:Key="BarNodeStyle">
            <Setter Property="ContextMenu" Value="{StaticResource testContextMenu}" />
        </Style>

        <Common:TreeViewStyleSelector
                x:Key="treeViewStyleSelector"
                FooNodeStyle="{StaticResource ResourceKey=FooNodeStyle}"
                BarNodeStyle="{StaticResource ResourceKey=BarNodeStyle}" />

    </Window.Resources>

    <StackPanel HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="517">
        <TreeView Height="100">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type Nodes:BarNode}" ItemsSource="{Binding Path=ChildNodes}">
                    <TextBlock Text="Bar" />
                </HierarchicalDataTemplate>

                <HierarchicalDataTemplate DataType="{x:Type Nodes:FooNode}" ItemsSource="{Binding Path=ChildNodes}">
                    <TextBlock Text="Foo" />
                </HierarchicalDataTemplate>
            </TreeView.Resources>

            <TreeViewItem Header="Testing" ItemsSource="{Binding Nodes}" ItemContainerStyleSelector="{StaticResource ResourceKey=treeViewStyleSelector}"/>
        </TreeView>
    </StackPanel>

</Window>

我在MSDN论坛上问了同样的问题,得到了一个我可以确认有效的答案

将ContextMenu对象放置在TextBlock上会提供相同的视觉外观,减去沿树节点链向下传递ContextMenu的效果

<HierarchicalDataTemplate DataType="{x:Type local:BarNode}" ItemsSource="{Binding Path=ChildNodes}">
    <TextBlock Text="Bar" ContextMenu="{StaticResource testContextMenu}" />
</HierarchicalDataTemplate>

我在MSDN论坛上问了同样的问题,得到了一个我可以确认有效的答案

将ContextMenu对象放置在TextBlock上会提供相同的视觉外观,减去沿树节点链向下传递ContextMenu的效果

<HierarchicalDataTemplate DataType="{x:Type local:BarNode}" ItemsSource="{Binding Path=ChildNodes}">
    <TextBlock Text="Bar" ContextMenu="{StaticResource testContextMenu}" />
</HierarchicalDataTemplate>