上下文菜单应仅出现在TreeView子节点上

时间:2013-07-17 16:15:57

标签: wpf xaml treeview wpf-controls contextmenu

我在TreeViewItems上有一个带有ContextMenu的TreeView。当我右键单击TreeViewItems时,contextMenu显得非常好。但我不希望ContextMenu出现在父项上。它应该只出现在子节点上。我在TreeView中只有一个hirarchy,即N个父母将M个孩子都在同一级别。这是我的TreeView和ContextMenu的XAML

   <TreeView Margin="2,0,0,0"  ItemsSource="{Binding SourceData}" Width="390">
        <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 Name="contextMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" >
                                        <MenuItem Name="menuItem" Header="Rename" Command="{Binding RenameCommand}" />
                                    </ContextMenu>
                     </Setter.Value>
                </Setter>
                <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                         </Trigger>
                </Style.Triggers>
                        </Style>
   </TreeView.ItemContainerStyle >

0 个答案:

没有答案