我想在右键单击时选择树视图的一个节点。我正在使用 MVVM 模式,不想在后面的代码中实现这一点。这是我用于树视图的 XAML。
<TreeView Margin="5,0,0,5" ItemsSource="{Binding TreePads}">
<TreeView.ItemContainerStyle >
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected" Value="{Binding DataContext.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource Self}}" />
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" >
<MenuItem IsEnabled="{Binding RenameMenuEnabled}" Header="Rename" Command="{Binding RenameCommand}" />
</ContextMenu>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type vm:TreePad}" ItemsSource="{Binding Members, Mode=TwoWay}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding PadName}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>