I didn't quite understand your question, but let's see if I got it right. You need to add content to the ContextMenu of the item based on the actions on the ViewModel, right?
Since the ContextMenu extends from MenuBase that extends from ItemsControl, you can add itens dynamically to it by using the ItemsSourceProperty (which is a DependencyProperty, so accept Bindings) so the ContextMenu you add a ItemsSource="{Binding ContextMenuItems}" and the ContextMenuItems property should be a collection of a new Type that contains the text that should be writen in the MenuItem of the ContextMenu and the ICommand that will be executed by it like:
public class Item : INotifyPropertyChanged
{
public ICommand Command
{
get;
private set;
}
private string _Name;
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
_Name = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
Then you need to add to the ContextMenu a DataTemplate for the Item that will be something like:
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Name}" Command="{Binding Command}"/>
</DataTemplate>
</ContextMenu.ItemTemplate>
then your Xaml Code will be something like:
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:custRootViewModel}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=name}">
<TextBlock.ContextMenu>
<ContextMenu ItemsSource="{Binding Path=ContextMenuItems}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Name}" Command="{Binding Command}"/>
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
Then for each Item in the Children Collection you have a collection of Items for the ContextMenu and using the Command Property you can trigger the action that you need for every specific scenario.
Just for the record you need to add the Command to the CommandBindings (which is a Collection of CommandBinding) of the control for this to work this is done by the use of either the static method CommandManager.RegisterClassCommandBinding(Type typeofthecontrol, new CommandBinding(ICommand command, ExecutedRoutedEventHandler Method, CanExecuteRoutedEventHandler Method)) or by the call of the Add method of the CommandBinding collection (UIElement.CommandBindings).
For further knowledge read:
http://msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.aspx[
^]
http://msdn.microsoft.com/en-us/library/system.windows.uielement.commandbindings.aspx[
^]
http://msdn.microsoft.com/en-us/library/system.windows.input.commandmanager.registerclasscommandbinding.aspx[
^]
http://msdn.microsoft.com/en-us/library/system.windows.controls.contextmenu.aspx[
^]
Hope this helps.
All best
Raul Mainardi Neto