silverlight - 自定义 XAML 属性

标签 silverlight dependency-properties attached-properties

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

将该库与我的数据库一起使用需要进行大量编码,而我现在无法真正完成这些编码。最终,这就是我想知道的......

我已经从我的 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"时,智能感知会识别它。我尝试将 typeof(string) 和 typeof(ownerclass) 设置为 HyperlinkBut​​ton 以查看是否有帮助,但没有帮助。有什么想法吗?

最佳答案

附加属性是实现它的方法。您应该定义这样的属性:

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 上尝试过。

关于silverlight - 自定义 XAML 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975069/

相关文章:

javascript - Silverlight 和 Flash javascript 桥兼容性

wpf - 当依赖属性改变时运行动画

wpf - 设置 DependencyProperty 值时,是在强制转换之前还是之后调用验证?

c# - WPF:在附加属性中,如何等待可视化树正确加载?

silverlight-4.0 - Silverlight 4 默认按钮服务

wpf - MVVM-如何绑定(bind)到不是 DependancyProperty 的属性?

c# - Silverlight 4 和存储过程

c# - 为什么 System.Numerics.BigInteger 在 Silverlight 4.0 中没有 Parse 方法,但在 .Net 4.0 中有?

c# - 如何将 HttpWebRequest 与 GET 方法一起使用

WPF DependencyProperty 链通知