l33t
l33t

Reputation: 19966

Pass static resource to custom System.Windows.Data.IValueConverter?

Consider the value converter below. I can easily pass values such as "Red" and "Green" to my converter, but how could I pass a brush defined in XAML?

How do I bind FalseBrush to MyNiceBrush?

<local:MyBrushConverter x:Key="BackgroundConverter" FalseBrush="Red" TrueBrush="Green" />

<LinearGradientBrush x:Key="MyNiceBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientStop Offset="0" Color="#4C7F00" />
    <GradientStop Offset="1" Color="#A0B529" />
</LinearGradientBrush>

In XAML, I bind my object's property to this converter: Background="{Binding MyClass.TrueOrFalseProperty, Converter={StaticResource BackgroundConverter} ...

Here's my converter:

public class MyBrushConverter : IValueConverter
{
    public Brush FalseBrush { get; set; }
    public Brush TrueBrush { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((bool)value)
            return TrueBrush;
        else
            return FalseBrush;
    }

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

Upvotes: 0

Views: 1144

Answers (1)

Zabavsky
Zabavsky

Reputation: 13640

<local:MyBrushConverter x:Key="BackgroundConverter" FalseBrush={StaticResource MyNiceBrush} TrueBrush="Green" />

Upvotes: 1

Related Questions