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
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.