欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > WPF 关于界面UI菜单权限(或者任意控件的显示权限)的简单管理--只是简单简单简单简单

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

2024/12/22 0:09:41 来源:https://blog.csdn.net/weixin_41440306/article/details/144486462  浏览:    关键词: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();

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com