且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在后面的代码中设置WPF UI权限

更新时间:2023-02-11 22:16:29

如果使用的是MVVM,则视图模型应具有诸如public bool IsXXXAllowed { get; private set; }之类的属性.实例化视图模型后,应查询用户是否具有使用XXX功能所需的最低权限,并相应地设置属性的值.理想情况下,权限检查应该与角色无关,因为可以为用户创建角色以进行嵌套等.您可以使用fn_my_permissions直接查询表上的权限.

If you're using MVVM, then your view model should have properties such as public bool IsXXXAllowed { get; private set; }. When the view model is instantiated you should query whether the user has the minimum permissions required to use XXX feature and set the value of the property accordingly. The permission check should ideally be agnostic of roles since roles can be user created for nesting etc. You can directly query the permissions on a table using fn_my_permissions.

然后在视图中将相关控件的IsEnabled属性(或使用BooleanToVisibilityConverterVisibility属性)简单绑定到属性.

Then in the view simply bind the IsEnabled property (or Visibility property using BooleanToVisibilityConverter) of the relevant control (or a Grid enclosing the controls if there is more than one control) to the IsXXXAllowed property.