简体   繁体   中英

Pass value of a StaticResource to ConverterParameter in a combine string

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:

在此处输入图片说明

Result at RunTime:

在此处输入图片说明

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?

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}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM