I think I understand your question. I think you can structure your ViewModels as follows:
interface ICommandViewModel : ICommand { string Name {get;} } interface INodeViewModel { IEnumerable<ICommandViewModel> CommandList {get;} } public class NodeViewModel : INodeViewModel { public NodeViewModel() {
and then in xaml
<TreeViewItem> <TreeViewItem.ContextMenu Items={Binding CommandList}> <ContextMenu.ItemTemplate> <DataTemplate> <MenuItem Header="{Binding Name}" Command="{Binding}"/> </DataTemplate> </ContextMenu.ItemTemplate> </TreeViewItem.ContextMenu> </TreeViewItem>
I do not have much experience working with a hierarchical data template, but you get the gist of the above. You can also do this with style, but I don't have the xaml editor in front of me to give you the correct syntax.
Hope that helps
Jose
source share