I have a TreeView with an hierarchical Data Template (2 levels). I have a context menu created on the first level of the tree view with 3 levels. I want to bind a command of my view model to the second level of the context menu. Unfortunately I can only get it to work when using a command in my Model, which is not what I want to do... If possible, I would like to do it all in XAML.
I have tested the pure xaml solutions given here and here. In the designer, the "Tag" is underlined blue saying
"Cannot resolve Property Tag in data context of type System.Windows.UIElement"or, if I use "Parent.PlacementTarget....", the designer tells me that
PlacementTarget cannot be resolved in the data context of type System.Windows.DependencyObject.The code is compileable, but my command is never reached.
This is what I have:
In a ResourceDictionary:
<DataTemplate DataType="{x:Type viewModel:UpdateToolViewModel}"> <view:UpdateToolView/> </DataTemplate> <DataTemplate x:Key="ToolNameDataTemplate" DataType="{x:Type src:Element}"> <Grid> <TextBlock Text="{Binding Path=NameProperty.Value}" FontSize="12" /> </Grid> </DataTemplate> <HierarchicalDataTemplate x:Key="ToolGroupsDataTemplate" ItemsSource="{Binding Elements}" DataType="{x:Type src:ElementGroup}" ItemTemplate="{StaticResource ToolNameDataTemplate}"> <TextBlock Text="{Binding Path=TextProperty.Value}" FontSize="14" FontWeight="Bold" Tag="{Binding ElementName=UpdateToolControl}"> <TextBlock.ContextMenu> <ContextMenu> <MenuItem Header="Add Tool" ItemContainerStyle="{StaticResource ToolGroupContextMenuToolsItemStyle}" > <MenuItem.ItemsSource> <CompositeCollection> <MenuItem Header="Add New ..." Command="{Binding PlacementTarget.Tag.DataContext.AddToolCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=ContextMenu}}" /> <CollectionContainer Collection="{Binding Source={StaticResource AddToolContextMenuSource}}"/> </CompositeCollection> </MenuItem.ItemsSource> </MenuItem> </ContextMenu> </TextBlock.ContextMenu> </TextBlock> </HierarchicalDataTemplate> In the UserControl:
<UserControl x:Class="...UpdateToolView" ... Name="UpdateToolControl"> <TreeView Name="ToolTreeView" ItemsSource="{Binding AllElementsInGroups}" ItemTemplate="{StaticResource ToolGroupsDataTemplate}" ItemContainerStyle="{StaticResource ToolTreeViewItemStyle}"/> </UserControl> I am already on the verge of using the command in my model, calling a method in my view model. Not nice, but I just don't seem to get it to work differently.
ContextMenuto yourUserControl. Then you Context menu will have the data context of yourUserControlwhich I assume is yourViewModel. REMEMBER:ContextMenuis NOT part of the Visual Tree. HTHResourceDictionaries, I try to keep the xaml code clean and structured. MyUserControlcontains a lot more xaml than shown in the example above. But I will keep your comment in mind. Thanks for this.