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

  • SEARCH
  • Home
  • 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 454445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:16:17+00:00 2026-05-12T22:16:17+00:00

Is there a way to specify in a TreeView ‘s HierarchicalDataTemplate to use a

  • 0

Is there a way to specify in a TreeView‘s HierarchicalDataTemplate to use a different ContextMenu depending on a property on the data an item is bound to?

For instance, display one ContextMenu if Item.IsFile is true, display a different one if Item.IsFolder is true, etc.

  • 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-12T22:16:18+00:00Added an answer on May 12, 2026 at 10:16 pm

    This is example for ListBox, I think you can easily modify it to work with TreeView.

    XAML:

    ...
    
    <Window.Resources>
        <ContextMenu x:Key="FileContextMenu">
            ...
        </ContextMenu>
        <ContextMenu x:Key="DirContextMenu">
            ...
        </ContextMenu>
    
        <local:ItemToContextMenuConverter x:Key="ContextMenuConverter" />        
    </Window.Resources>
    
    ...
    
    <ListBox x:Name="SomeList">
        <ListBox.ItemTemplate>
            <DataTemplate>                          
                <Label Content="{Binding Path=Name}" ContextMenu="{Binding Converter={StaticResource ContextMenuConverter}}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    Code:

    class Item
    {
        public string Name { get; set; }
        public bool IsFile { get; set; }
    }
    
    [ValueConversion(typeof(Item), typeof(ContextMenu))]
    public class ItemToContextMenuConverter : IValueConverter
    {
        public static ContextMenu FileContextMenu;
        public static ContextMenu DirContextMenu;
    
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Item item = value as Item;
            if (item == null) return null;
    
            return item.IsFile ? FileContextMenu : DirContextMenu;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ItemToContextMenuConverter.FileContextMenu 
                = this.Resources["FileContextMenu"] as ContextMenu;
            ItemToContextMenuConverter.DirContextMenu 
                = this.Resources["DirContextMenu"] as ContextMenu;
    
            List<Item> items = new List<Item>();
            items.Add(new Item() { Name = "First", IsFile = true });
            items.Add(new Item() { Name = "Second", IsFile = false });
    
            SomeList.ItemsSource = items;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to specify which object to use for global when invoking
Is there a way to specify in a data connection or the LinqToSql Designer
Is there a way to specify which local network interface to use when using
Is there a way to specify how long data is held in HttpContext.Cache?
is there a way to specify different cell editor for each cell in jface
Is there any way to specify different font sizes for fallback fonts in CSS?
Is there a way to specify a table to use for Hi-Lo values, with
Is there a way to specify the natural ordering of data in mongodb, similar
Is there any way to specify table column width as is. In other words,
Is there a way to specify the maximum recursion level for recursive diff diff

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.