Pass value of a StaticResource to ConverterParameter in a combine string

4.3k views Asked by At

I wrote a converter that get

  • Value as Bool
  • Parameter as String

I use it like this:

BorderBrush="{Binding IsSelected,
                      Converter={StaticResource BoolToColorBrushConverter},
                      ConverterParameter='#ff00bfff;#0000bfff'}"

If Value is True then the converter return a ColorBrush from first color Hex code in Parameter else return a ColorBrush from second color hex code.

My converter work very well But I want know how can i use it like this:

<Color x:Key="MyColor1">#66bb66</Color>

--------------------

BorderBrush="{Binding IsSelected,
                      Converter={StaticResource BoolToColorBrushConverter},
                      ConverterParameter=#ff00bfff;{StaticResource MyColor1}}"

Result in Design mode:

enter image description here

Result at RunTime:

enter image description here

But i need color Hex code of the StaticResource in my parameter like this:

Parameter: "#ff00bfff;#66bb66"

My question is how can i pass a StaticResource value in a combine string to my ConverterParameter???

What is your solution?

1

There are 1 answers

0
SunnyHoHoHo On

I know it's a bit late, but hopefully this can help the late visitors:

here is the converter code:

public class BoolToBorderBrushConverter : IValueConverter
{
    public SolidColorBrush TrueColor { get; set; }

    public SolidColorBrush FalseColor { get; set; }

    // this example compares a binding property (string) with 1 parameter (also in string)
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null && parameter != null)
        {
            if (String.Compare(value.ToString(), parameter.ToString(), true) == 0)
            {
                return this.TrueColor;
            }
            else
            {
                return this.FalseColor;
            }
        }
        return this.FalseColor;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

then you can configure the converter in xaml like this (in the ResourceDictionary section):

<ResourceDictionary>
    <local:BoolToBorderBrushConverter x:Key="BrushConverter" TrueColor="{StaticResource MyTrueColor}" FalseColor="Transparent">
</ResourceDictionary>

and this is how you use the converter:

<Border BorderBrush="{Binding MyProperty, Converter={StaticResource BrushConverter}, ConverterParameter=ABC}"/>