2

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.

2
  • have you tried binding it to CommandParameter Commented Feb 3, 2013 at 13:43
  • @ethicallogics That is actuallly part of the question. 1st I dont know how to bind it to commandparameter. And 2nd I dont know how to pass NOT selected, but just r-clikcked element. Commented Feb 3, 2013 at 13:48

3 Answers 3

2

Hi this is just a way you can bind

 <ContextMenu Name="MyContextMenu" DataContext="{Binding PlacementTarget,RelativeSource={RelativeSource Self}}"> <MenuItem Header="Add" Command="{Binding DataContext.AddNewItemCommand}" CommandParameter="{Binding }"/> </ContextMenu> 

I hope this will help.

Sign up to request clarification or add additional context in comments.

15 Comments

This is good, but it can be used only for selected item. I need to detect rightclicked item
for that you can use the PlacementTarget Property of the ContextMenu
same result I think there must be some property that indicates is mouse over the node. Bute using MVVM I dont know where to place it but codebehind
I am not sure whats wrong but PlacementTarget guna solve your problem if on right click ContextMenu opens
What this PlacementTarget exactly must do?
|
2

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.

Comments

0

You can use the code found here to detect the item under the right click (and select it for positive visual feedback).

Follow this link for an explanation of what PlacementTarget is doing in the solution provided by @ethicallogics.

Hopefully you can use a combination of the two answers to solve your problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.