devios1
devios1

Reputation: 38035

'System.Windows.Data.MultiBinding' is not a valid value for property 'Text'

I'm trying to write a custom MarkupExtension that allows me to use my own mechanisms for defining a binding, however when I attempt to return a MultiBinding from my MarkupExtension I get the above exception.

I have:

<TextBlock Text="{my:CustomMarkup ...}" />

CustomMarkup returns a MultiBinding, but apparently Text doesn't like being set to a MultiBinding. How come it works when I say:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding ... />
    </TextBlock.Text>
</TextBlock>

But it doesn't work the way I'm doing it?

Upvotes: 1

Views: 1003

Answers (2)

devios1
devios1

Reputation: 38035

Did some investigating, and it turns out I'm supposed to actually set the binding in the MarkupExtension's ProvideValue method and then return the current value of the binding. A little counter-intuitive but so far seems to be working!

Here's roughly what I ended up with:

public override object ProvideValue( IServiceProvider serviceProvider ) {
    IProvideValueTarget valueProvider = (IProvideValueTarget)serviceProvider.GetService( typeof( IProvideValueTarget ) );
    // only need to do this if they're needed in your logic:
    object @target = valueProvider.TargetObject;
    object @property = valueProvider.TargetProperty;

    MultiBinding result = new MultiBinding();

    // set up binding as per custom logic...

    return result.ProvideValue( serviceProvider );
}

Add a little logic, dust lightly with error handling and serve.

Update: Turns out MultiBinding.ProvideValue() hooks up the binding itself, based on the target and property information in serviceProvider. That's much cleaner.

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292685

Don't return the MultiBinding itself. Instead, return the result of MultiBinding.ProvideValue.

BTW, what exactly are you doing in your markup extension ? Perhaps you could just inherit from MultiBinding, if you don't need to override ProvideValue (which is sealed). You can achieve almost anything by just setting the appropriate Converter and other properties

Upvotes: 2

Related Questions