WPF Xaml标记TextToBrushConverter字典<string,brush>可能吗?

时间:2018-02-15 08:41:31

标签: c# wpf xaml converter

我的目标是根据数据源的某个Text属性更改Control的Background(Brush)。

当我的转换器只有2个画笔属性时,我在一个更简单(工作)的例子中发生了这种情况:

<local:ListErrorWarndescriptionToColorConverter x:Key="ErrorListToColor"
                        NormalBrush="Transparent" ErrorBrush="Red" WarnBrush="Yellow"/>

下一步是编写该转换器以处理2个以上的字符串

我的转换器代码:

public class StringToBrushDictionary : Dictionary<string, Brush> { }

[ValueConversion(typeof(string), typeof(Brush))]
public sealed class TextToBrushConverter : IValueConverter
{

    public StringToBrushDictionary LookUpTable { get; set; }
    public Brush Default { get; set; }

    public TextToBrushConverter()
    {
        // set defaults
        LookUpTable = new StringToBrushDictionary();
        Default = Brushes.Transparent;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var collection = value as string;
        if (collection == null)
            return Default;
        if (LookUpTable.ContainsKey(collection))
            return LookUpTable[collection];
        return Default;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

我现在的问题是:如何在xaml中填写该字典 我发现this 8 year old question但它并不完全是我需要的,因为它使用字符串和int,两个“本机”数据类型,并且还使用VS 2010,它不支持字典(根据那里的答案)。有一个答案说明后来的XAML版本可以做到这一点,但VS 2010还没有实现它。

以下是我对转换器Markup的看法以及我已尝试过的内容:

<Application x:Class="BetterListbox.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:BetterListbox"
              xmlns:System="clr-namespace:System;assembly=mscorlib"
             xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
             xmlns:Specialized="clr-namespace:System.Collections.Specialized;assembly=System"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis" />
        <local:ListErrorWarndescriptionToColorConverter x:Key="ErrorListToColor"
                                                        NormalBrush="Transparent" ErrorBrush="Red" WarnBrush="Yellow"/>

        <local:TextToBrushConverter x:Key="WarnErrorToBrush" Default="Red">
            <local:TextToBrushConverter.LookUpTable>
                <local:StringToBrushDictionary>
                    <Brush x:Key="Error" >
                        ...?                        
                    </Brush>
                    <collections:DictionaryEntry x:Key="d"  Value="Brushes.Red" />

                </local:StringToBrushDictionary>
            </local:TextToBrushConverter.LookUpTable>
        </local:TextToBrushConverter>
    </Application.Resources>
</Application>

甚至可以在xaml中填充LookUpTable,如果是,还可以填写如何?

1 个答案:

答案 0 :(得分:1)

你可以这样填写:

<my:TextToBrushConverter x:Key="textToBrush">
    <my:TextToBrushConverter.LookUpTable>
        <!-- LookUpTable["red"] = Brushes.Red -->
        <x:Static MemberType="Brushes" Member="Red" x:Key="red" />
        <!-- LookUpTable["aqua"] = new SolidColorBrush(Colors.Aqua) -->
        <SolidColorBrush Color="Aqua" x:Key="aqua" />
        <!-- custom color brush -->
        <SolidColorBrush Color="#234433" x:Key="green" />
    </my:TextToBrushConverter.LookUpTable>
</my:TextToBrushConverter>