简体   繁体   English

WPF-需要结合使用Tree + Grid和上下文菜单

[英]WPF - Need a combination of Tree+Grid, with Context Menu

My application is implemented by a GridView inside a TreeList . 我的应用程序是通过TreeListGridView实现的。

在此处输入图片说明

Much to my despair, I discovered that the GridView is very primitive, compared to the widely used DataGrid . 令我感到绝望的是,与广泛使用的DataGrid相比,GridView非常原始。 I am considering these two options: 我正在考虑以下两种选择:

(1) Somehow, I replace the GridView with a DataGrid (which supports Context Menu). (1)不知何故,我将GridView替换为DataGrid(支持Context Menu)。

(2) Somehow, I add the Context Menu capability to the existent GridView. (2)不知何故,我将上下文菜单功能添加到了现有的GridView中。

Which of the 2 approaches (or another?) would you recommend? 您会推荐两种方法中的哪一种(或另一种?)?

Source code is much appreciated. 源代码非常受赞赏。

TIA. TIA。

Based on the linked code, here is the solution: 根据链接的代码,以下是解决方案:

1 - Add the ContextMenu as a Resource: 1-将ContextMenu添加为资源:

<Window.Resources>
    <ContextMenu x:Key="ItemsContextMenu" x:Shared="False">
        <MenuItem>
            <MenuItem.Header>
                <TextBlock>
                    <Run>Context Menu Action for Item</Run>
                    <Run Text="{Binding Tag.Name}"/>
                </TextBlock>
            </MenuItem.Header>
        </MenuItem>
    </ContextMenu>

    <!-- other stuff here -->

</Window.Resources>

It is recommended that you set x:Shared="False" to prevent Binding issues related to reusing the resource instance. 建议您设置x:Shared="False"以防止与重用资源实例有关的绑定问题。

2 - Define an ItemContainerStyle for your TreeList that sets the ContextMenu for the TreeListItem s: 2 -定义ItemContainerStyle您的TreeList该设置文本菜单的TreeListItem S:

<tree:TreeList ...>
    <!-- other stuff here -->

    <tree:TreeList.ItemContainerStyle>
        <Style TargetType="{x:Type tree:TreeListItem}">
            <Setter Property="ContextMenu" Value="{StaticResource ItemsContextMenu}"/>
         </Style>
    </tree:TreeList.ItemContainerStyle>
</tree:TreeList>

Notice that I'm using DataBinding in the ContextMenu , which means you have a proper, working DataContext in it. 请注意,我在ContextMenu使用了DataBinding,这意味着您在其中具有适当的,可以正常工作的DataContext You should be able to use Commands and other stuff in it. 您应该可以使用其中的Commands和其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM