Extending Binding class to provide a bindable converter parameter

307 views Asked by At

I need to create a string in a particular format, for which I use IMultiValueConverter. Example:

{0} of {1} in {2}
SomeValue0
SomeValue1
SomeValue2

Results in:

SomeValue0 of SomeValue1 in SomeValue2

This part is not a problem. Basically converter accepts multiple strings, of which the first one is the string format, and later ones are the strings to format.

<MultiBinding Converter={...}>
  <Binding>
    <Binding.Source>{0} of {1} in {2}</Binding.Source>
  </Binding>
  <Binding Path="Value0" />
  <Binding Path="Value1" />
  <Binding Path="Value2" />
</MultiBinding>

It gets tricky when some of the strings (Binding) also require the use of IMultiValue converter. Imagine that property #Value1# has different value for different language. Normally we get such value also using IMultiValueConverter:

<TextBox>
  <TextBox.Text>
    <MultiBinding Converter={...}>
      <Binding Path="Value1"?
      <Binding Path="Strings" Source="{StaticResource langResources}" />
    </MultiBinding>
  </TextBox.Text>
</TextBox>

When the user switches to different language, a dictionary of Strings is updated, and TextBox receives new value (the same applies to changing a value to Value1).

Now the problem: It is not possible to use IMultiValueConverter inside IMultiValueConverter. You also cannot override ProvideValue for BindingBase, and IMultiValueConverter will accept only the objects of type BindingBase.

Is there any way I can somehow extend BindingBase so that it exposes a BindableConverterProperty, whose value will be used to provide value from Binding?

1

There are 1 answers

3
ΩmegaMan On

tricky when some of the strings (Binding) also require the use of IMultiValue converter.

Why not put the intelligence of the string's IMultivalue converter into a specialized class who's ToString() will output the correct string based on the current environmental settings? That would eliminate the need for the sub multivalue converter you describe but work within the framework of the parent MultiValue converter.

The super-classed string class I recommend simply has to subscribe to an event which informs it which dictionary to use. Then when ToString() is called, the correct string is passed on to the top level converter.