Treeview上的上下文菜单未在所选项目上打开

时间:2021-01-04 20:30:17

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?

当我右键单击某个项目时,没有菜单出现,但现在当我右键单击某个节点时,菜单会出现,但是在之前右键单击的项目位置上。除非您再次右键单击任何项​​目,否则菜单不会被忽略。有什么帮助吗?

1 个解决方案

#1


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>

#1


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>