示例:WPF中如何绑定ContextMenu和Menu

一、目的:开发过程中,有些模块的右键ContextMenu菜单是需要动态显示的,既是根据不同条件显示不同的菜单,很多是通过代码去生成ContextMenu的MenuItem,本文介绍通过绑定的方式去加载ContextMenu,Menu菜单栏的同样适用,原理同TreeView的绑定

二、效果

三、环境

VS2022

四、实现

实现代码

XML 复制代码
            <TextBlock Text="右键弹出菜单">
                <TextBlock.ContextMenu>
                    <ContextMenu ItemsSource="{local:ClassTypeTreeDataProvider IsRecursion=True, Type={x:Type FrameworkElement}}">
                        <ContextMenu.ItemContainerStyle>
                            <Style TargetType="MenuItem">
                                <Setter Property="Command" Value="{local:ShowDialogCommand}"/>
                                <Setter Property="CommandParameter" Value="{Binding Model}"/>
                            </Style>
                        </ContextMenu.ItemContainerStyle>
                        <ContextMenu.ItemTemplate>
                            <HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
                                <DockPanel>
                                    <CheckBox VerticalAlignment="Center" IsChecked="{Binding IsChecked, Mode=TwoWay}" />
                                    <TextBlock  VerticalAlignment="Center" Text="{Binding Model.Name}" />
                                </DockPanel>
                            </HierarchicalDataTemplate>
                        </ContextMenu.ItemTemplate>
                    </ContextMenu>
                </TextBlock.ContextMenu>
            </TextBlock>

原理跟TreeView一样,应用ItemsSource绑定数据源,应用HierarchicalDataTemplate模板作为ItemTemplate来绑定,树结构的绑定参考文档

示例:WPF中TreeView自定义TreeNode泛型绑定对象-CSDN博客

示例:WPF中TreeView自定义TreeNode泛型绑定对象来实现级联勾选-CSDN博客

Menu的绑定原理一样

实现代码

XML 复制代码
            <Menu ItemsSource="{local:ClassTypeTreeDataProvider IsRecursion=True, Type={x:Type FrameworkElement}}">
                <Menu.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="Command" Value="{local:ShowDialogCommand}"/>
                        <Setter Property="CommandParameter" Value="{Binding Model}"/>
                    </Style>
                </Menu.ItemContainerStyle>
                <Menu.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
                        <TextBlock  VerticalAlignment="Center" Text="{Binding Model.Name}" />
                    </HierarchicalDataTemplate>
                </Menu.ItemTemplate>
            </Menu>

当切换ItemsSource既可以实现动态切换ContextMenu

五、需要了解的知识点

ContextMenu 类 (System.Windows.Controls) | Microsoft Learn

Menu 类 (System.Windows.Controls) | Microsoft Learn

HierarchicalDataTemplate 类 (System.Windows) | Microsoft Learn

MenuItem 类 (System.Windows.Controls) | Microsoft Learn

ICommand 接口 (System.Windows.Input) | Microsoft Learn

六、源码地址

GitHub - HeBianGu/WPF-ControlDemo: 示例

GitHub - HeBianGu/WPF-ControlBase: Wpf封装的自定义控件资源库

GitHub - HeBianGu/WPF-Control: WPF轻量控件和皮肤库

七、了解更多

System.Windows.Controls 命名空间 | Microsoft Learn

https://github.com/HeBianGu

HeBianGu的个人空间-HeBianGu个人主页-哔哩哔哩视频

相关推荐
三千道应用题7 小时前
WPF学习笔记(13)列表框控件ListBox与数据模板
wpf
甄天9 天前
WPF中MVVM和MVVMLight模式
c#·wpf·visual studio
CoderIsArt12 天前
C# WPF常用调试工具汇总
开发语言·c#·wpf
凉、介12 天前
SylixOS 下的消息队列
linux·wpf·sylixos
Magnum Lehar12 天前
wpf3d游戏引擎ProjectLayoutView实现
游戏引擎·wpf
摆烂的少年12 天前
WPF中自定义DataGrid表格后,修改选中行的字体颜色或背景图
wpf
CoderIsArt12 天前
WPF调试三种工具介绍:Live Visual Tree、Live Property Explorer与Snoop
wpf
甄天12 天前
WPF Style样式 全局样式资源字典
c#·wpf
GalaxyPokemon13 天前
RPC - 服务注册与发现模块
wpf
FuckPatience14 天前
WPF 的RenderTransform使图标旋转180°
wpf