WPF - 需要Tree + Grid与上下文菜单的组合 [英] WPF - Need a combination of Tree+Grid, with Context Menu

查看:337
本文介绍了WPF - 需要Tree + Grid与上下文菜单的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序由 GridView TreeList 中实现。

My application is implemented by a GridView inside a TreeList.

对于我的绝望,我发现GridView是非常原始的,与广泛使用的 DataGrid 相比。我正在考虑这两个选项:

Much to my despair, I discovered that the GridView is very primitive, compared to the widely used DataGrid. I am considering these two options:

(1)不知何故,我用DataGrid(支持上下文菜单)替换GridView。

(1) Somehow, I replace the GridView with a DataGrid (which supports Context Menu).

(2)不知何故,我将Context Menu功能添加到现有的GridView。

(2) Somehow, I add the Context Menu capability to the existent GridView.

你会推荐哪种方法(或另一种?)? ?

Which of the 2 approaches (or another?) would you recommend?

源代码非常感谢。

TIA。

推荐答案

根据链接代码,这里是解决方案:

Based on the linked code, here is the solution:

1 - 添加 ContextMenu 作为资源:

1 - Add the ContextMenu as a Resource:

<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>

建议您设置 x:Shared =False 以防止与重用资源实例相关的绑定问题。

It is recommended that you set x:Shared="False" to prevent Binding issues related to reusing the resource instance.

2 - 定义一个 ItemContainerStyle 对于 TreeListItem s:

2 - Define an ItemContainerStyle for your TreeList that sets the ContextMenu for the TreeListItems:

<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>


$ b c>,这意味着你有一个正确的,工作的 DataContext 。您应该可以使用命令和其他内容。

Notice that I'm using DataBinding in the ContextMenu, which means you have a proper, working DataContext in it. You should be able to use Commands and other stuff in it.

这篇关于WPF - 需要Tree + Grid与上下文菜单的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆