pangabiMC
pangabiMC

Reputation: 784

MultiBinding with StringFormat - Why is the targetType == object in the inner converter?

I'm binding two strings using Multibinding and StringFormat to a TextBox. I've noticed something strange when I add a Converter to one of the inner bindings, like this:

<TextBox>
    <TextBox.Text>
        <MultiBinding StringFormat="{}{0}  {1} ">
            <Binding Path="Foo" 
                        Converter="{StaticResource someConverter}" 
                        ConverterParameter="true" />
            <Binding Path="Bar"  />
        </MultiBinding>
    </TextBox.Text>
</TextBox>

In the converter the TargetType property is going to be object. Using the same converter directly on a Text property (without the multibinding) it is string.

I'm wondering why is that happening, and if there is any way of letting the converter know about the type of the parent binding (not sure if the terminology is correct here).

The reason I'm asking is that I'm usually checking the to/from types in the converters and I return Binding.DoNothing if there is a mismatch.

Upvotes: 0

Views: 556

Answers (1)

Lukas Kubis
Lukas Kubis

Reputation: 929

When you use the someConverter directly on a Text property, the TargetType will be String, because it's based on the Type of Text property. But when you use a converter in Binding which is placed inside MultiBinding, there is no information about TargetType and Object is used by default.

Maybe you are familiar with IMultiValueConverter, so when you use it, the TargetType will be String as you expected. Look at the example bellow:

<TextBox>
  <TextBox.Text>
    <MultiBinding StringFormat="{}{0}  {1} " Converter="{StaticResource someMultiConverter}" ConverterParameter="true" >
      <Binding Path="Foo" />
      <Binding Path="Bar"  />
    </MultiBinding>
  </TextBox.Text>
</TextBox>

Upvotes: 2

Related Questions