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 6838423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:35:43+00:00 2026-05-26T23:35:43+00:00

I have TreeView which contains different item types. Item Styles are defined over a

  • 0

I have TreeView which contains different item types. Item Styles are defined over a custom ItemContainerStyleSelector property.

My styles are all sharing a base style and only item specific stuff is defined in each style. It looks like this:

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

<Style x:Key ="SomeSpecificStyle" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource BaseStyle}">
   <Setter Property="ContextMenu" Value="{StaticResource NodeContextMenu}"/>
   ...  
</Style>

<Style x:Key ="SomeSpecificStyle" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource BaseStyle}">
   <Setter Property="ContextMenu" Value="{StaticResource AnotherNodeContextMenu}"/>
   ...  
</Style>

The context menu is defined like this

<ContextMenu x:Key="NodeContextMenu">
  <MenuItem Header="Select Views" Command="{Binding Path=OpenViewsCommand}" />
  ...other specific entries
  <MenuItem Header="Remove" Command="{Binding Path=DocumentRemoveCommand}" />
  ...other entries common for all menus
</ContextMenu>

Another context menu also should contain those common items like remove. These need to be replicated by copy paste every time the command properties, etc are changing. A hell for maintainability. Is there a way to define a context menu which contains the common items, and then “derive” specific context menus?

Edit: I found a solution with the hints from this thread:
I define a Collection with the common items, and use a composite collection when defining a menu to include both new items and the common items collection

<CompositeCollection x:Key="CommonItems"> 
  <MenuItem Header="Remove" Command="{Binding Path=DocumentRemoveCommand}">
  ....Other common stuff
</CompositeCollection>

<ContextMenu x:Key="NodeContextMenu">
  <ContextMenu.ItemsSource>
    <CompositeCollection>
      <MenuItem Header="Select Views" Command="{Binding Path=OpenViewsCommand}" />
      <CollectionContainer Collection="{StaticResource CommonItems}" />
    </CompositeCollection>
  </ContextMenu.ItemsSource>
</ContextMenu>
  • 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-05-26T23:35:43+00:00Added an answer on May 26, 2026 at 11:35 pm

    You can declare the items as resource and reference them:

    <Some.Resources>
        <MenuItem x:Key="mi_SelectViews" x:Shared="false"
                  Header="Select Views" Command="{Binding Path=OpenViewsCommand}" />
        <MenuItem x:Key="mi_Remove" x:Shared="false"
                  Header="Remove" Command="{Binding Path=DocumentRemoveCommand}" />
    </Some.Resources>
    
    <ContextMenu x:Key="NodeContextMenu">
      <StaticResource ResourceKey="mi_SelectViews" />
      ...other specific entries
      <StaticResource ResourceKey="mi_Remove" />
      ...other entries common for all menus
    </ContextMenu>
    

    (The x:Shared is important)


    Another possibility would be to generate MenuItems via a object model approach, you just bind the ItemsSource to some list of objects which model the functionality of a MenuItem (i.e. properties for child items, header and command), then you can create one Remove model which can be part of multiple lists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a treeview (winforms) which have different item types on it. I have
I have a TreeView control which displays two things: 1) Folder 2) Item Where
I have a TreeView which contains items populated by a HierarchicalDataTemplate. I am trying
I have a UserControl which contains a TreeView and a TextBlock : <TreeView ItemsSource={Binding
I have an app written in XUL which contains a custom tree view that
I have a TreeView, which contains nodes. When a user clicks on a node,
In my project, I have TreeView, which contains a tree of objects of various
I have a TreeView in my form for which I dont want to have
I have GUI application with gtk.Treeview component. It's model is set to gtk.Treestore, which
I have WPF treeView which has ControlTemplate which show every node of treeView consisting

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.