将Enum转换为TextBlock文本内的字符串 [英] Convert Enum to string inside TextBlock text

查看:72
本文介绍了将Enum转换为TextBlock文本内的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的 Enum :

public enum StatusMessage
{
    Cancel,
    Done,
    [Description("In process...")]
    InProcess,
    [Description("We have delay...")]
    Delay,
    Waiting
}

GridViewColumn :

我的财产:

StatusMessage StatusMsg;

XAML:

<GridViewColumn Width="180" Header="Status" >
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding StatusMsg}" Foreground="{Binding StatusMsg,Converter={my:StatusMessageToColorConverter}}" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

我有这个 EnumToStringConverter :

public class EnumToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string EnumString;
        try
        {
            EnumString = Enum.GetName((value.GetType()), value);
            return EnumString;
        }
        catch
        {
            return string.Empty;
        }
    }

    // No need to implement converting back on a one-way binding 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

现在我想在我的 TextBlock 中使用此 Convertor :

Now i want to use this Convertor inside my TextBlock :

<TextBlock Text="{Binding StatusMsg, Converter={my:EnumToStringConverter}}" Foreground="{Binding StatusMsg,Converter={my:StatusMessageToColorConverter}}" />

所以问题是我有这个错误:

So the problem is that i have this error:

'my:EnumToStringConverter'用作标记扩展名,但确实不是从MarkupExtension派生的.

'my:EnumToStringConverter' is used like a markup extension but does not derive from MarkupExtension.

这是什么 MarkupExtension ?

推荐答案

您需要在XAML中声明EnumToStringConverter的实例.它可以是本地资源,也可以在app.xaml中声明以使其可在任何地方访问.

You need to declare an instance of the EnumToStringConverter in XAML. It can be a local resource or declared in app.xaml to make it accessible everywhere.

<Window.Resources>
    <my:EnumToStringConverter x:Key="DefaultEnumToStringConverter"/>
</Window.Resources>

然后像这样使用它:

Text="{Binding StatusMsg, Converter={StaticResource DefaultEnumToStringConverter}}"

请注意转换器中的"StaticResource"一词.那是标记扩展.这告诉WPF使用键"DefaultEnumToStringConverter"查找静态资源.WPF将搜索元素的可视树以查找具有该键的资源.如果找不到,它将在 app.xaml 中的应用程序级别进行检查.

Note the word "StaticResource" in the converter. That is the markup extension. This one tells WPF to go find the static resource with the key "DefaultEnumToStringConverter". WPF will search up the visual tree of the element looking for a resource with that key. If one isn't found it will check at the application level in app.xaml.

MarkupExtensions是{},"x","binding","static"等中包含的属性开头的内容.它们使WPF能够将text属性解析为有用的对象实例.您可以创建自己的MarkupExtensions来完成一些非常酷的事情.

MarkupExtensions are the things at the beginning of an attribute enclosed in the {}, "x", "binding", "static", etc. They are what give WPF the ability to resolve the text attribute in to a useful object instance. You can create your own MarkupExtensions to do some pretty cool things.

在您的特定示例中,它抱怨是因为它正在内部 Converter = {my:EnumToStringConverter} 中寻找名为"my"的标记扩展名.

In your particular example it is complaining because it is looking for a markup extension named "my", from the inner Converter={my:EnumToStringConverter}.

这篇关于将Enum转换为TextBlock文本内的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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