结合枚举类型的文本框 [英] Binding enum type to textbox

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

问题描述

文本值枚举类型。
我的枚举看起来像

text value to enum type. My enum looks like that

public enum Type
    {

        Active,

        Selected,

        ActiveAndSelected
    }

我wan't到acomplish是显示在文本框增强模式,而不是激活等。是否有可能这样做吗?这将是巨大的,如果我能acomplish在XAML - 因为所有的绑定我在样式文件style.xaml

What I wan't to acomplish is to show on textbox "Active Mode" instead of "Active" and so on. Is it possible to do that? It would be great if I could acomplish that in XAML - because all bindings I have in style file style.xaml

我试图用描述属性,但似乎这还不够。

I was trying to use Description attributes but it seems that it's not enough

推荐答案

恕我直言,使用转换器是一个更好的办法。

IMHO, using a converter is a better approach.

你应该做的第一件事是为一些元数据添加到您的枚举元素实现一个简单的属性。这里有一个基本的例子(没有国际化的简单):

The first thing you should do is implement a simple attribute in order to add some metadata to your enum elements. Here's a basic example (without internationalization for simplicity):

    public enum StepStatus {
    [StringValue("Not done yet")]
    NotDone,
    [StringValue("In progress")]
    InProgress,
    [StringValue("Failed")]
    Failed,
    [StringValue("Succeeded")]
    Succeeded
}

接下来这一点,你可以写一个工具类能够从一个enum元素转换为使用反射其相应的StringValue重新presentation。搜索在谷歌为字符串枚举在C# - $ C $的CProject,你会发现$ C $的CProject的关于这篇文章(对不起,我的低信誉不会让我添加链接。)

Next to that, you can write a utility class able to convert from an enum element to its corresponding StringValue representation using reflection. Search in Google for "String Enumerations in C# - CodeProject" and you'll find CodeProject's article about this (sorry, my low reputation won't let me add the link..)

现在,你可以实现一个转换器,它只是代表转换为实用类:

Now you can implement a converter that simply delegates the conversion to the utility class:

    [ValueConversion(typeof(StepStatus), typeof(String))]
public class StepStatusToStringConverter: IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture){
        String retVal = String.Empty;

        if (value != null && value is StepStatus) {
            retVal = StringEnum.GetStringValue((StepStatus)value);
        }

        return retVal;
    }

    /// <summary>
    /// ConvertBack value from binding back to source object. This isn't supported.
    /// </summary>
    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture) {
        throw new Exception("Can't convert back");
    }
}

最后,您可以使用转换器在你的XAML code:

Finally, you can use the converter in your XAML code:

<resourceWizardConverters:StepStatusToStringConverter x:Key="stepStatusToStringConverter" />
...
<TextBox Text="{Binding Path=ResourceCreationRequest.ResourceCreationResults.ResourceCreation, Converter={StaticResource stepStatusToStringConverter}}" ... />

检查下面的页面;它提供了一个支持国际化的例子,但基本原理是一样的。

Check the following page; it gives an example that supports internationalization, but basically the principle is the same..

这篇关于结合枚举类型的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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