Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:01:56+00:00 2026-06-14T10:01:56+00:00

I am trying to make design a WPF TreeView where the ContextMenu activates on

  • 0

I am trying to make design a WPF TreeView where the ContextMenu activates on particular nodes.

In my example, despite my best efforts, I cannot keep the ContextMenu of a BarNode from appearing when it’s children ‘FooNode’s are clicked.

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:01:58+00:00Added an answer on June 14, 2026 at 10:01 am

    I asked the same question of the MSDN Forums and got an answer that I can confirm as working.

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

    Placing the ContextMenu objects on the TextBlock gives the same visual appearance, minus the effect of passing the ContextMenu down the chain of TreeNodes.

    <HierarchicalDataTemplate DataType="{x:Type local:BarNode}" ItemsSource="{Binding Path=ChildNodes}">
        <TextBlock Text="Bar" ContextMenu="{StaticResource testContextMenu}" />
    </HierarchicalDataTemplate>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a design decision. I'm not well acquainted with either MongoDB
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I'm trying to make an app, in C# .NET 4.0 and WPF, that indexes:
I'm trying to make responsive design. When screen is small comments should be below
I am trying to make use of someone else's cms design as he stores
I'm trying to make a design for some sort of IExecutable interface. I will
I am trying to make decision how to design my app. I have about
I'm trying to make a 2 column design (using Twitter Bootstrap ) with 2
guys im trying to make a dynamic design for adding students in a class
I am trying to make the following design for a web site. The last

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.