【问题标题】:Custom XAML property自定义 XAML 属性
【发布时间】:2011-02-11 23:24:09
【问题描述】:

我看到一个库允许我在我的 XAML 中执行此操作,它根据用户是否处于角色中来设置控件的可见性: s:Authorization.RequiresRole="Admin"

在我的数据库中使用该库需要大量编码,而我现在真的无法做到。最终这就是我想知道的......

我从我的 SPROC 收到了经过身份验证的用户角色,并且它当前作为属性存储在我的 App.xaml.cs 中(对于最终解决方案来说不是必需的,现在仅供参考)。我想创建一个属性(依赖属性?附加属性?),它允许我说一些与其他库非常相似的东西:RequiresRole="Admin",如果用户不是管理员角色,它会折叠可见性。谁能指出我正确的方向?

编辑 构建授权类后,出现以下错误: "在 XML 命名空间 clr-namespace:TSMVVM.Authorization 中的类型 'HyperlinkBut​​ton' 上不存在属性 'RequiredRole'"

我正在尝试添加这个 xaml:

<HyperlinkButton x:Name="lnkSiteParameterDefinitions" 
        Style="{StaticResource LinkStyle}" 
                                  Tag="SiteParameterDefinitions" 
        Content="Site Parameter Definitions" 
        Command="{Binding NavigateCommand}"
        s:Authorization.RequiredRole="Admin"
        CommandParameter="{Binding Tag, ElementName=lnkSiteParameterDefinitions}"/>

当我开始输入 s:Authorization.RequiredRole="Admin" 时,intellisense 将其拾取。我尝试将 typeof(string) 和 typeof(ownerclass) 设置为 HyperlinkBut​​ton 以查看是否有帮助,但没有。有什么想法吗?

【问题讨论】:

    标签: silverlight dependency-properties attached-properties


    【解决方案1】:

    附加属性是实现它的方式。你应该像这样定义一个属性:

    public class Authorization
    {
        #region Attached DP registration
    
        public static string GetRequiredRole(UIElement obj)
        {
            return (string)obj.GetValue(RequiredRoleProperty);
        }
    
        public static void SetRequiredRole(UIElement obj, string value)
        {
            obj.SetValue(RequiredRoleProperty, value);
        }
    
        #endregion
    
        // Using a DependencyProperty as the backing store for RequiredRole.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RequiredRoleProperty =
            DependencyProperty.RegisterAttached("RequiredRole", typeof(string), typeof(Authorization), new PropertyMetadata(RequiredRole_Callback));
    
        // This callback will be invoked when some control will receive a value for your 'RequiredRole' property
        private static void RequiredRole_Callback(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            var uiElement = (UIElement) source;
            RecalculateControlVisibility(uiElement);
    
            // also this class should subscribe somehow to role changes and update all control's visibility after role being changed
        }
    
        private static void RecalculateControlVisibility(UIElement control)
        {
            //Authorization.UserHasRole() - is your code to check roles
            if (Authentication.UserHasRole(GetRequiredRole(control)))
                control.Visibility = Visibility.Visible;
            else 
                control.Visibility = Visibility.Collapsed;
        }
    }
    

    PS:发现您询问 Silverlight 为时已晚。虽然我相信它在那里的工作方式相同,但我只在 WPF 上尝试过。

    【讨论】:

    • 感谢您的回复!这看起来不错,但我对实现有一些疑问.. 1) typeof(ownerclass) - 我将其设置为授权,但我不确定这是否正确。 2) 新的 UIPropertyMetadata ...这与新的 PropertyMetadata 相同吗? UIPropertyMetadata 没有为我解决。 3) 我创建了 UserHasRole 方法,如果 App.Role.ToLower() 与传入的 role.ToLower() 相同,则简单地返回 true。听起来对吗? 4) 在我的 OP 中添加了一个编辑,因为我在构建时遇到了错误。
    • @Scott ownerClass - 1) 授权,我在回答中对其进行了编辑。 2) 是的,Silverlight 没有可用的 UIPropertyMetadata,可以使用 PropertyMetadata 3) 是
    • 知道为什么我会收到添加到原始帖子中的错误吗?
    • 从健身房和晚餐回来后,错误就不存在了。我之前肯定已经重建过,但也许我做了或没有做其他事情。让它工作的另一个问题(它在用户登录之前比较角色)但我会问另一个问题。谢谢雪熊!
    猜你喜欢
    • 2017-10-05
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多