wpf 将 Enum 转换为 TextBlock 文本中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34337755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:35:22  来源:igfitidea点击:

Convert Enum to string inside TextBlock text

wpfxamlenums

提问by david hol

I have simple Enum:

我有简单的Enum

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

And GridViewColumn:

并且GridViewColumn

My property:

我的财产:

StatusMessage StatusMsg;

XAML:

XAML:

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

And i have this EnumToStringConverter:

我有这个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();
    }
}

Now i want to use this Convertorinside my TextBlock:

现在我想Convertor在我的里面使用它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' is used like a markup extension but does not derive from MarkupExtension.

'my:EnumToStringConverter' 用作标记扩展,但不是从 MarkupExtension 派生的。

What is this MarkupExtension?

这是什么MarkupExtension

回答by Bradley Uffner

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.

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

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

Then use it like this:

然后像这样使用它:

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

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.

请注意转换器中的“StaticResource”一词。那就是标记扩展。这告诉 WPF 使用键“DefaultEnumToStringConverter”查找静态资源。WPF 将搜索元素的可视化树,寻找具有该键的资源。如果未找到,它将在app.xaml.

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.

MarkupExtensions 是包含在 {}、“x”、“binding”、“static”等中的属性开头的东西。它们使 WPF 能够将文本属性解析为有用的对象实例。您可以创建自己的 MarkupExtensions 来做一些非常酷的事情。

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

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