[英]Context menu on Treeview does not open on selected item
I have a treeview with a context menu and I am using an converter to manage it. 我有一个带有上下文菜单的树视图,并且正在使用转换器对其进行管理。 I do not want the menu to open on items and just want it to open on nodes.
我不希望菜单在项目上打开,而只希望它在节点上打开。
<BooleanToVisibilityConverter x:Key="VisibilityConverter" />
<ContextMenu x:Key="AddNew" Name="PopMnu" Visibility="{Binding IsFolder,Converter={StaticResource VisibilityConverter}}">
<MenuItem Header="New Symbol..." Click="AddSymbolMenu_Click"/>
<MenuItem Header="New Folder..." Click="NewFolderItem_Click"/>
</ContextMenu>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="ContextMenu" Value="{StaticResource AddNew}"/>
</Style>
When I right click on an item, no menu comes up but now when I right click on a node, the menu comes up but on the location of the item previously right clicked. 当我右键单击一个项目时,没有菜单出现,但是现在当我右键单击一个节点时,菜单出现,但是在先前右键单击该项目的位置上。 Also the menu does not dismiss unless you right click again on any item.
除非您再次右键单击任何项目,否则菜单不会关闭。 Any help please?
有什么帮助吗?
As Krishna's comment suggested, a solution would be to have a view model for a folder and one for an item 正如克里希纳(Krishna)的评论所建议的那样,一种解决方案是为文件夹提供一个视图模型,为项目提供一个视图模型。
public class Folder : ViewModelBase { }
public class Item : ViewModelBase { }
Then you can define a DataTemplate for each, one containing a context menu the other without. 然后,您可以为每个定义一个DataTemplate,一个包含一个上下文菜单,另一个不包含。
<TreeView Name="SymbolsTreeView" ItemsSource="{Binding Items}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Folder}" ItemsSource="{Binding Items}">
<Grid Background="Red">
<Grid.ContextMenu>
<ContextMenu>
</ContextMenu>
</Grid.ContextMenu>
<TextBlock Text="{Binding Name}"/>
</Grid>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:Item}" >
<Grid >
<TextBlock Text="{Binding Name}"/>
</Grid>
</DataTemplate>
</TreeView.Resources>
</TreeView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.