【问题标题】:WPF - Need a combination of Tree+Grid, with Context MenuWPF - 需要 Tree+Grid 和上下文菜单的组合
【发布时间】:2014-06-26 15:51:22
【问题描述】:

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

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

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

(2) 不知何故,我将上下文菜单功能添加到现有的 GridView。

您会推荐这两种方法中的哪一种(或另一种?)?

非常感谢源代码。

TIA。

【问题讨论】:

  • 这是一个很好的问题,我需要实现一些非常相似的东西。
  • HighCore:你是说通过添加名为SharedSizeGroup 的东西,TreeView 就可以拥有上下文菜单了吗?
  • 我刚刚读到SharedSizeGroup 与列宽有关:这是我现在最少的问题。我需要一个ContextMenu,它由DataGrid 实现,但不是GridView
  • 如果你有足够的钱来消费 DevExpress 会为你提供所需的控制 - TreeList Control
  • @swiss_programmer 查看我的回答。顺便说一句,请编辑您的问题,发布相关代码,或者至少提及您创建了一个名为 TreeList 的类,这是一个允许分层数据的专用 ListView。这将帮助未来的读者理解上下文,而无需下载您的代码并查看它

标签: c# wpf datagrid wpf-controls


【解决方案1】:

根据链接代码,解决方案如下:

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>

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

2 - 为您的 TreeList 定义一个 ItemContainerStyle,为 TreeListItems 设置 ContextMenu:

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

请注意,我在ContextMenu 中使用了DataBinding,这意味着您在其中有一个正确的、有效的DataContext。您应该可以使用Commands 和其中的其他内容。

【讨论】:

  • 高核摇滚!高核规则!! :^D ps: 不要走得太远... :-)
猜你喜欢
  • 1970-01-01
  • 2022-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多