Geo...
Geo...

Reputation: 307

Setting dependency properties on a static value converter from XAML

I have a value converter I wrote that allows me to bind against a property, test that property against a given (hard-coded) value, and return a brush based on if the test was true or false. The converter inherits from DependencyObject and implements IValueConverter. It exposes two dependency properties called PositiveBrush and NegativeBrush.

I declare it in XAML like this:

<UserControl.Resources>
        <xyz:CBrushConverter x:Key="BrushConverter"
                             PositiveBrush="{DynamicResource Glyph.Resource.Brush.LightGreen}"
                             NegativeBrush="{DynamicResource Glyph.Resource.Brush.DarkGray}" />
</UserControl.Resources>

I can then adjust the color of a given element like this:

<TextBlock Foreground="{Binding SomeProperty, ConverterParameter='SomeValue', Converter={StaticResource BrushConverter}}" />

So in this example (making the assumption that SomeProperty returns a string) if the bound property 'SomeProperty' matches 'SomeValue' the converter will return the PositiveBrush as the Foreground (otherwise it will return the NegativeBrush).

So far so good - There may be other ways to skin this cat; but this has served me well for a long time and I don't really want to rock the boat.

What I would like to do however is declare my Positive and Negative brushes as part of my binding expression. Right now, if I wanted to use Red/Green and Blue/Yellow color combinations, I would need to declare two BrushConverters. But if I could declare the Positive/Negative brushes as part of the binding expression, I could use the same converter.

In pseudo-code, something like this (obviously this doesn't work):

<Grid Foreground="{Binding SomeProperty, ConverterParameter='SomeValue', Converter={StaticResource BrushConverter, BrushConverter.PositiveBrush='Red', BrushConverter.NegativeBrush='Green'}}" />

I did find a similar question on stack, How can I set a dependency property on a static resource? but it didn't explicitly address my question.

So... my google-foo is weak - I wasn't able to come up with the right search terms to dissect the Xaml binding syntax and work this out on my own, if it is even possible.

As always, any help is appreciated!

Upvotes: 0

Views: 566

Answers (2)

Clemens
Clemens

Reputation: 128061

This should work:

<TextBlock>
    <TextBlock.Foreground>
        <Binding Path="SomeProperty" ConverterParameter="SomeValue">
            <Binding.Converter>
                <xyz:CBrushConverter PositiveBrush="Red" NegativeBrush="Green"/>
            </Binding.Converter>
        </Binding>
    </TextBlock.Foreground>
</TextBlock>

Note however that you don't use the converter as static resource here. You would create a new converter instance for each Binding.

Upvotes: 2

mm8
mm8

Reputation: 169200

But if I could declare the Positive/Negative brushes as part of the binding expression, I could use the same converter.

You can't really do this. Converter is just a property of the Binding class. You still need to create an instance of the converter and set the dependency properties of this particular instance. What if you have several bindings that uses the same converter instance with different values for the PositiveBrush and NegativeBrush properties simultaneously?

You could define a converter instance inline though:

<TextBlock>
    <TextBlock.Foreground>
        <Binding Path="SomeProperty" ConverterParameter="SomeValue">
            <Binding.Converter>
                <xyz:CBrushConverter PositiveBrush="Green" NegativeBrush="Red" />
            </Binding.Converter>
        </Binding>
    </TextBlock.Foreground>
</TextBlock>

Upvotes: 1

Related Questions