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

The Archive Base Latest Questions

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

I have a TreeView I’m populating and adding a ContextMenu to each item. The

  • 0

I have a TreeView I’m populating and adding a ContextMenu to each item. The problem is in my ViewModel the TreeView ItemSource is bound to a property on the ViewModel itself. When I attempt to reference some property on the ViewModel again I can’t seem to get it to work.

<TreeView Grid.ColumnSpan="1" Grid.Row="1" HorizontalAlignment="Stretch" ItemsSource="{Binding ModelItems}" SelectedTreeItem="{Binding SelectedItem, Mode=TwoWay}" VerticalAlignment="Stretch" Grid.RowSpan="3" Margin="5">
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Models}">
        <TextBlock Text="{Binding Header, Mode=TwoWay}"  ToolTip="{Binding Tooltip, Mode=TwoWay}">
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Server" Visibility="{Binding Path=IsServerVisible}">
                        <MenuItem Header="Add" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Windows:MainWindow}}, Path=ViewModel:ViewModel.AddServerCommand}"/>
                        <MenuItem Header="Edit" />
                        <MenuItem Header="Delete" />
                    </MenuItem>
                    <MenuItem Header="Config" Visibility="{Binding Path=IsConfigVisible}">
                        <MenuItem Header="Fetch" />
                        <MenuItem Header="Edit" />
                        <MenuItem Header="Save" />
                    </MenuItem>     
                </ContextMenu>
            </TextBlock.ContextMenu>
        </TextBlock>
    </HierarchicalDataTemplate>                
</TreeView.ItemTemplate>

A previous post on StackOverflow pointed me in the direction of using the RelativeSource to bind correctly to my ViewModel on the MainWindow. However when I run the application the command is not working and the Output window is not generating any binding or xaml errors that I can see.

Basically the Visibility bindings work because those properties exist on the “Models” items. However I want everything to be moved to the ViewModel especially the Command.

Can anyone spot what I’ve done incorrectly here?

  • 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:34:24+00:00Added an answer on June 14, 2026 at 10:34 am

    The key thing to remember here is context menus are not part of the visual tree.

    Therefore they don’t inherit the same source as the control they belong to for binding. The way to deal with this is to bind to the placement target of the ContextMenu itself. But since you want to bind it for Command in ViewModel class, place the DataContext in Tag of your TextBlock and use in your command Binding like this –

    <HierarchicalDataTemplate ItemsSource="{Binding Models}">
        <TextBlock Text="{Binding Header}"
                   Tag="{Binding DataContext, RelativeSource=
                   {RelativeSource Mode=FindAncestor, AncestorType=Window}}">
             <TextBlock.ContextMenu>
                  <ContextMenu>
                      <MenuItem Header="Server" Command="{Binding 
                                PlacementTarget.Tag.AddServerCommand,
                                RelativeSource={RelativeSource Mode=FindAncestor, 
                                AncestorType=ContextMenu}}"/>
                   </ContextMenu>
              </TextBlock.ContextMenu>
          </TextBlock>
    </HierarchicalDataTemplate>
    

    Use for other bindings similarly like above and it will work how you want.

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

Sidebar

Related Questions

I have a treeview that is bound to a collection and each item in
I have a treeview that is bound to a database table. Each treeview item
I have a TreeView that is bound to an ObservableCollection in my ViewModel. I
I have a Treeview control with each item having a checkbox. At root level
I have a TreeView which I have bound to a ViewModel called RootViewModel which
I have a TreeView that launches a new window when each of its TreeViewItems
I have a TreeView that is bound to a XmlDataSource control. I've added some
I have a TreeView control on a form. I am dynamically adding new TreeNodes
I have a treeview (winforms) which have different item types on it. I have
I have TreeView Populating from Database And I am Trying to detect the user

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.