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

1.定义你的User类

cs 复制代码
 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.定义你的权限类别

cs 复制代码
  public enum AccountType
  {
     None, Operator, Administrator, Developer,Master
  }

3.实现附加属性

cs 复制代码
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.使用方法

XML 复制代码
<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实现刷新

cs 复制代码
   App.User?.Set(a => { a.AccountType = AccountType.Master; });
   AccountManager.Refresh?.Invoke();
相关推荐
勘察加熊人25 分钟前
wpf+c#路径迷宫鼠标绘制
开发语言·c#·wpf
OneByOneDotNet28 分钟前
WPF 教程:给 TreeView 添加 SelectedItem 双向绑定支持(MVVM-Friendly)
wpf
猫猫不是喵喵.4 小时前
vue 路由
前端·javascript·vue.js
烛阴5 小时前
JavaScript Import/Export:告别混乱,拥抱模块化!
前端·javascript
bin91535 小时前
DeepSeek 助力 Vue3 开发:打造丝滑的表格(Table)之添加行拖拽排序功能示例12,TableView16_12 拖拽动画示例
前端·javascript·vue.js·ecmascript·deepseek
拉不动的猪6 小时前
vue自定义“权限控制”指令
前端·javascript·vue.js
魔云连洲7 小时前
Vue2和Vue3响应式的基本实现
开发语言·前端·javascript·vue.js
CreatorRay7 小时前
受控组件和非受控组件的区别
前端·javascript·react.js
JSON_L8 小时前
Vue 组件通信 - Ref组件通信
javascript·vue.js·ecmascript
Invisible_He8 小时前
iOS自定义collection view的page size(width/height)分页效果
ui·ios·swift·collection