当前位置: 首页 > news >正文

WPF 关于界面UI菜单权限(或者任意控件的显示权限)的简单管理--只是简单简单简单简单

1.定义你的User类

 public class User{public User(){ID = ObjectId.NewObjectId().ToString();}public string? ID { get; set; }public string? Account { get; set; }public string? Password { get; set; }public string? PasswordMD5 { get; set; }public AccountType? AccountType { get;set; }public string? Name { get; set; }public string? CardNumer { get; set; }public string? Sex { get; set; }public DateTime? RegistrationTime { get; set; }public DateTime? LoginTime { get; set; }public string? Description { get; set; }}

2.定义你的权限类别

  public enum AccountType{None, Operator, Administrator, Developer,Master}

3.实现附加属性

internal class AccountManager{internal static HashSet<FrameworkElement> OnAccountTypeChangeToDoList = new HashSet<FrameworkElement>();internal static Action? Refresh { get; set; }internal static User? User { get => App.User; }internal static AccountType GetAccountType(DependencyObject obj){return (AccountType)obj.GetValue(AccountTypeProperty);}internal static void SetAccountType(DependencyObject obj, AccountType value){obj.SetValue(AccountTypeProperty, value);}internal static string GetGuid(DependencyObject obj){return (string)obj.GetValue(GuidProperty);}internal static void SetGuid(DependencyObject obj, string value){obj.SetValue(GuidProperty, value);}// Using a DependencyProperty as the backing store for Guid.  This enables animation, styling, binding, etc...internal static readonly DependencyProperty GuidProperty =DependencyProperty.RegisterAttached("Guid", typeof(string), typeof(AccountManager), new PropertyMetadata(""));// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...internal static readonly DependencyProperty AccountTypeProperty =DependencyProperty.RegisterAttached("AccountType", typeof(AccountType), typeof(AccountManager), new PropertyMetadata(AccountType.None, OnAccountTypeChanged));private static void OnAccountTypeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e){AccountType type = (AccountType)e.NewValue;var element = obj as FrameworkElement;if (element != null){element.Visibility = User?.AccountType >=type ? Visibility.Visible : Visibility.Collapsed;if (!OnAccountTypeChangeToDoList.Contains(element)){OnAccountTypeChangeToDoList.Add(element);Refresh+= () =>{element.Visibility = User?.AccountType >= type ? Visibility.Visible : Visibility.Collapsed;};}}}}

4.使用方法

<Window x:Class="Test.Windows.TestView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Test.Windows"mc:Ignorable="d"  xmlns:Common="clr-namespace:Test.Common"Title="TestView" Height="450" Width="800"><StackPanel><Button Content="用户按钮" Common:AccountManager.AccountType="Operator"/><Button Content="管理员按钮" Common:AccountManager.AccountType="Administrator"/></StackPanel>
</Window>

5.如果界面初始化之后,后台更改User类型之后可以调用Refresh实现刷新

   App.User?.Set(a => { a.AccountType = AccountType.Master; });AccountManager.Refresh?.Invoke();

http://www.lryc.cn/news/505121.html

相关文章:

  • Https身份鉴权(小迪网络安全笔记~
  • AngularJS 输入验证
  • 【网络安全】WIFI WPA/WPA2协议:深入解析与实践
  • 前端使用xlsx-js-style导出Excel,带样式,并处理合并单元格边框显示不全和动态插入表头解决
  • 自动化工具ansible部署和实践
  • 无人机推流直播平台EasyDSS视频技术如何助力冬季森林防火
  • React Fiber
  • 【前端】JavaScript 中的 map() 方法:高级解析与应用
  • 《智能体开发实战(高阶)》四、系统化的日志周报智能体开发计划
  • 什么是Apache日志?为什么Apache日志分析很重要?
  • Mybatis——(2)
  • 景联文科技入选中国信通院发布的“人工智能数据标注产业图谱”
  • 修改浏览器地址栏参数
  • Spring Boot教程之二十五: 使用 Tomcat 部署项目
  • 解决 Git 默认不区分文件名大小写的问题
  • python学opencv|读取图像(十二)BGR图像转HSV图像
  • 信息安全工程师-选择题考点总结
  • 重学SpringBoot3-WebClient配置与使用详解
  • springBoot中的日志级别在哪里配置
  • 统一身份安全管理体系的业务协同能力
  • JAVA课堂笔记23(IO流 (java.io包中))
  • C# DLT645 97/07数据采集工具
  • 中后台管理信息系统:Axure12套高效原型设计框架模板全解析
  • Reactor 响应式编程(第四篇:Spring Security Reactive)
  • JVM 双亲委派模型以及垃圾回收机制
  • Delphi编写涂鸦桌面的小程序
  • 智星云技术文档:GPU测速教程
  • 《Kali Linux 软件源更换攻略:优化软件获取与系统更新》
  • C# 在dataview可以直接增删改查mysql数据库
  • C#—泛型约束