1

我正在尝试设计一个 WPF TreeView,其中 ContextMenu 在特定节点上激活。

在我的示例中,尽管我尽了最大努力,但我无法阻止 BarNode 的 ContextMenu 在其子项 'FooNode' 被单击时出现。

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

1 回答 1

1

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

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7dd183bc-d616-4ec4-8b2a-0b438c9a115c

将 ContextMenu 对象放置在 TextBlock 上会提供相同的视觉外观,但会减去将 ContextMenu 沿 TreeNode 链传递的效果。

<HierarchicalDataTemplate DataType="{x:Type local:BarNode}" ItemsSource="{Binding Path=ChildNodes}">
    <TextBlock Text="Bar" ContextMenu="{StaticResource testContextMenu}" />
</HierarchicalDataTemplate>
于 2012-11-02T02:32:21.917 回答