Привязка Command ContextMenu в HierarchicalDataTemplate TreeView
<TreeView Focusable="False" Name="TreeViewCocos" Background="#87BECE" Grid.Column="2" ItemsSource="{Binding Path=RectItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:StandartRectangelItem}" ItemsSource="{Binding Path=Items, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsChecked}"/>
<TextBlock Text="{Binding Path=Uid}" FontSize="16">
<TextBlock.ContextMenu>
<ContextMenu DataContext="{Binding RelativeSource={RelativeSource Self}}">
<MenuItem Header="" Command={Binding ???} CommandParameter={Binding ???}/>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<Button Margin="5,0,0,0" Content="Copy" Width="27" Height="16" FontSize="10" Command="{Binding ElementName=TreeViewCocos, Path=DataContext.CopyNodeInfoCommand}" CommandParameter="{Binding Path=Node}"/>
<Button Margin="5,0,0,0" Content="Get" FontSize="10" Height="16" Width="20" Command="{Binding ElementName=TreeViewCocos, Path=DataContext.ShowNodeInfoCommand}" CommandParameter="{Binding Path=Node}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding ElementName=TreeViewCocos, Path=DataContext.IsExpandAllNodes, Mode=TwoWay}"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
Как через ContextMenu привязать Command, чтобы она отработала? Какой Context ей задать?
В примере с Button Command="{Binding ElementName=TreeViewCocos, Path=DataContext.CopyNodeInfoCommand}" CommandParameter="{Binding Path=Node}" это работает, но в ContextMenu нет
Ответы (1 шт):
Автор решения: Денис Матюшкин
→ Ссылка
<TreeView.Resources>
<ContextMenu x:Key="ContextMenuTreeView">
<MenuItem Header="Test" Command="{Binding PlacementTarget.Tag.DataContext.ShowNodeInfoCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" CommandParameter="{Binding}"/>
</ContextMenu>
</TreeView.Resources>
<TextBlock Text="{Binding Path=Uid}" Tag="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}" FontSize="16" ContextMenu="{DynamicResource ContextMenuTreeView}"/>