WPF - 需要树+电网的组合,与上下文菜单(WPF - Need a combination o

2019-10-20 09:06发布

我的应用程序是通过实现GridView一个内部TreeList

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

(1)不知何故,我替换数据网格中的GridView(支持上下文菜单)。

(2)不知怎的,我的上下文菜单功能添加到存在的GridView控件。

这2种方法(或者其他?),你会推荐?

源代码是非常赞赏。

TIA。

Answer 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" ,以防止有关重用资源实例绑定的问题。

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>

请注意,我使用数据绑定在ContextMenu ,这意味着你有一个正确的,工作DataContext它。 您应该能够使用Commands和其他的东西在里面。



文章来源: WPF - Need a combination of Tree+Grid, with Context Menu