自定义 XAML 属性

发布于 2024-10-17 07:52:01 字数 1058 浏览 6 评论 0原文

我见过一个库,允许我在 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" ,智能感知拾取了它。我尝试将 typeof(string) 和 typeof(ownerclass) 设置为 HyperlinkBut​​ton 以查看是否有帮助,但没有帮助。有什么想法吗?

I've seen a library that allows me to do this inside my XAML, which sets the visibility of the control based on whether or not the user is in a role:
s:Authorization.RequiresRole="Admin"

Using that library with my database requires a bunch of coding that I can't really do right now. Ultimately here's what I want to know...

I have received the authenticated users role from my SPROC, and its currently stored in my App.xaml.cs as a property (not necessary for the final solution, just FYI for now). I want to create a property (dependency property? attached property?) that allows me to say something very similar to what the other library has: RequiresRole="Admin", which would collapse the visibility if the user is not in the Admin role. Can anyone point me in the right direction on this?

EDIT
After building the authorization class, I get the following error:
"The property 'RequiredRole' does not exist on the type 'HyperlinkButton' in the XML Namespace clr-namespace:TSMVVM.Authorization"

I'm trying to add this 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}"/>

When I started typing the s:Authorization.RequiredRole="Admin", intellisense picked it up. I tried setting the typeof(string) and typeof(ownerclass) to HyperlinkButton to see if that would help, but it didn't. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2024-10-24 07:52:01

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

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

Attached property is the way to implement it. You should define a property like this:

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: Have noticed too late that you were asking about Silverlight. Though I believe it works in the same way there, but I've tried it only on WPF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文