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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:28:08+00:00 2026-06-18T12:28:08+00:00

Good daytime. I’ve got a problem with creating ContextMenu of TreeView . The problem

  • 0

Good daytime.

I’ve got a problem with creating ContextMenu of TreeView. The problem is very simple. I want to add new items to treeview clicking RMB on treeviewitem and selecting a context menu command.
I know that I need to pass to my command a parameter that contains parent item. BUT. I need that I can RMB click on any treeviewitem, not only selected.
And heres the question:
How to pass the binded data of treeviewitem to my command.

Here is class diag
enter image description here

Here is Xaml (EDIT)

        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Item.Children}">
                <TextBlock Text="{Binding Item.Code}" HorizontalAlignment="Stretch">
                    <TextBlock.ContextMenu>
                        <ContextMenu Name="MyContextMenu" DataContext="{Binding PlacementTarget,RelativeSource={RelativeSource Self}}">
                            <MenuItem Header="{Binding DataContext.ToString()}" Command="{Binding DataContext.Item.AddNewItemCommand}" CommandParameter="{Binding}"/>
                        </ContextMenu>                            
                    </TextBlock.ContextMenu>
                </TextBlock>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>

But it does not even call my command.

    private void AddNewItem(object toItem)
    {
        if (toItem == null)
            return;
        ItemViewModel item = toItem as ItemViewModel;
        ItemMaterialModel itemMaterial = new ItemMaterialModel(ItemModel.CreateNewItem());

        ItemMaterialViewModel itemMaterialViewModel = new ItemMaterialViewModel(itemMaterial);
        item.Children.Add(itemMaterialViewModel);
    }

Maybe my command in wrong ViewModel?

Regards, Dmitry.

  • 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-18T12:28:09+00:00Added an answer on June 18, 2026 at 12:28 pm

    Thanks to ethicallogics and his info about PlacementProperty, I’ve modified my Xaml like here:

            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Item.Children}">
                    <TextBlock Text="{Binding Item.Code}" HorizontalAlignment="Stretch">
                        <TextBlock.ContextMenu>
                            <ContextMenu DataContext="{Binding PlacementTarget.DataContext,RelativeSource={RelativeSource Mode=Self}}">
                                <MenuItem 
                                    Header="{Binding Item.Code}"
                                    Command="{Binding Item.AddNewItemCommand}" 
                                          CommandParameter="{Binding Item}"/>
                            </ContextMenu>                            
                        </TextBlock.ContextMenu>
                    </TextBlock>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
    

    And in my StructureManagerViewModel I made not simple MainItem, but a collection of MainItems used in itemssourse of tree.

    Regards, Dmitry.
    Hope this experience will help people.

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

Sidebar

Related Questions

Good day. I try to add new tab with navigation controller to my app.
Good day, I am new to phonegap and JQM... I have a problem with
Good morning, I need a hook or something to call to separate new posts
Good Morning, i tried to add the same action listener for both a JComboBox
Good day! I have a quite strange problem. I have installed joomla on one
Good evening! I have a little problem accessing custom classes in symfony2.1.6 using the
Good Day, I'm new to this Youtube GData api and i'm having trouble with
Good day to everybody I have a simple counter php+db it checks ref number
Good day. Having read about Model 2 architecture I got confused about some points.
Good daytime to all I am confused with fork() . Does fork() forks child

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.