码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 示例:WPF中如何绑定ContextMenu和Menu


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


    二、效果

    三、环境
    VS2022

    四、实现

    实现代码

    1. <TextBlock Text="右键弹出菜单">
    2. <TextBlock.ContextMenu>
    3. <ContextMenu ItemsSource="{local:ClassTypeTreeDataProvider IsRecursion=True, Type={x:Type FrameworkElement}}">
    4. <ContextMenu.ItemContainerStyle>
    5. <Style TargetType="MenuItem">
    6. <Setter Property="Command" Value="{local:ShowDialogCommand}"/>
    7. <Setter Property="CommandParameter" Value="{Binding Model}"/>
    8. Style>
    9. ContextMenu.ItemContainerStyle>
    10. <ContextMenu.ItemTemplate>
    11. <HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
    12. <DockPanel>
    13. <CheckBox VerticalAlignment="Center" IsChecked="{Binding IsChecked, Mode=TwoWay}" />
    14. <TextBlock VerticalAlignment="Center" Text="{Binding Model.Name}" />
    15. DockPanel>
    16. HierarchicalDataTemplate>
    17. ContextMenu.ItemTemplate>
    18. ContextMenu>
    19. TextBlock.ContextMenu>
    20. TextBlock>

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

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

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

    Menu的绑定原理一样

    实现代码

    1. <Menu ItemsSource="{local:ClassTypeTreeDataProvider IsRecursion=True, Type={x:Type FrameworkElement}}">
    2. <Menu.ItemContainerStyle>
    3. <Style TargetType="MenuItem">
    4. <Setter Property="Command" Value="{local:ShowDialogCommand}"/>
    5. <Setter Property="CommandParameter" Value="{Binding Model}"/>
    6. Style>
    7. Menu.ItemContainerStyle>
    8. <Menu.ItemTemplate>
    9. <HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
    10. <TextBlock VerticalAlignment="Center" Text="{Binding Model.Name}" />
    11. HierarchicalDataTemplate>
    12. Menu.ItemTemplate>
    13. 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个人主页-哔哩哔哩视频

  • 相关阅读:
    软文发稿费用大概多少?发一篇软文要多少钱?
    torch.nn.interpolate—torch上采样和下采样操作
    RabbitMQ:路由模式
    java实用代码-----HttpsUtil
    vue3——使用axios
    打造个性化代购商城,源码解决方案助您独占海外购物市场
    Linux常用命令
    RabbitMQ —— 理解及应用场景
    《C和指针》笔记34:字符串函数
    ps2022 - ps to dxf
  • 原文地址:https://blog.csdn.net/u010975589/article/details/139834079
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号