在ConverterParameter使用枚举 [英] Using enum in ConverterParameter

查看:774
本文介绍了在ConverterParameter使用枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立,可以由多个用户使用的应用程序。每个用户被划分到下一个身份验证级别之一:

I am building an application that can be used by many users. Each user is classified to one of the next Authentication levels:

public enum AuthenticationEnum
{
    User,
    Technitian,     
    Administrator,
    Developer
}

有些控件(如按钮)是只暴露某些用户级别。
我有保持当前用户的身份验证级别的属性:

Some controls (such as buttons) are exposed only to certain levels of users. I have a property that holds the authentication level of the current user:

public AuthenticationEnum CurrentAuthenticationLevel { get; set; }



我想这个属性的一些控件的可见性属性绑定和参数传递给所述转换器的方法,告诉它什么是最低验证电平即能看到控制。
例如:

I want to bind this property to the 'Visibilty' property of some controls and pass a parameter to the Converter method, telling it what is the lowest authentication level that is able to see the control. For example:

<Button Visibility="{Binding Path=CurrentAuthenticationLevel, Converter={StaticResource AuthenticationToVisibility}, ConverterParameter="Administrator"}"/>



意味着,只有管理员和开发者可以看到按钮。
不幸的是,上面的代码通过管理员作为一个字符串。当然,我可以转换方法内用户的switch-case和字符串转换为AuthenticationEnum。但是,这是丑陋的,而且容易维护错误(每次枚举变化 - 转炉法应该改变)。

means that only 'Administrator' and 'Developer' can see the button. Unfortunately, the above code passes "Administrator" as a string. Of course I can user Switch-Case inside the converter method and convert the string to AuthenticationEnum. But this is ugly and prone to maintenance errors (each time the enum changes - the converter method should change).

有没有更好的方式来传递不平凡的对象作为参数?

Is there a better way to pass not trivial object as a parameter?

推荐答案

ArsenMkrt的答案是正确的,

ArsenMkrt's answer is correct,

的另一种方法这样做是为了使用x:静态语法在 ConverterParameter

Another way of doing this is to use the x:Static syntax in the ConverterParameter

<Button ...
        Visibility="{Binding Path=CurrentAuthenticationLevel,
            Converter={StaticResource AuthenticationToVisibility},
            ConverterParameter={x:Static local:AuthenticationEnum.Administrator}}"/>

和转炉

public class AuthenticationToVisibility : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        AuthenticationEnum authenticationEnum = (AuthenticationEnum)parameter;
        //...
    }
}

这篇关于在ConverterParameter使用枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆