How can I pass a constant value for 1 binding in multi-binding?

WpfBindingMultibinding

Wpf Problem Overview


I have a multi-binding like

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource myConverter}">
            <Binding Path="myFirst.Value" />
            <Binding Path="mySecond.Value" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

And I want to pass a fixed value e.g. "123" to one of the two bindings above. How can I do that using XAML?

Wpf Solutions


Solution 1 - Wpf

If your value is simply a string, you can specify it as a constant in the Source property of a binding. If it is any other primitive data type, you need to define a static resource and reference this.

Define the sys namespace in the root of the XAML to point to System in mscorlib, and the following should work:

<TextBlock>
  <TextBlock.Resources>
    <sys:Int32 x:Key="fixedValue">123</sys:Int32>
  </TextBlock.Resources>
  <TextBlock.Text>
    <MultiBinding Converter="{StaticResource myConverter}">
      <Binding Path="myFirst.Value" />
      <Binding Source="{StaticResource fixedValue}" />
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

Solution 2 - Wpf

Or, combining the two answers above:

<MultiBinding Converter="{StaticResource ScalingConverter}">
    <Binding>
        <Binding.Source>
            <sys:Double>0.5</sys:Double>
        </Binding.Source>
    </Binding>
    <Binding ElementName="TC" Path="ActualWidth" />
</MultiBinding>

Which provides the right type without the Resources kludge.

Solution 3 - Wpf

I don't quite follow the question but there are two options:

Put the line <Binding Source="123" /> in your multibinding will pass 123 as a bound value to your converter.

Put ConverterParameter="123" in your MultiBinding:

<MultiBinding Converter="{StaticResource conv}" ConverterParameter="123">

Solution 4 - Wpf

I'm not saying this an especially good answer but here is another approach:

<Binding Path="DoesNotExist" FallbackValue="123" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNam G VUView Question on Stackoverflow
Solution 1 - WpfNoldorinView Answer on Stackoverflow
Solution 2 - WpfAnders KaplanView Answer on Stackoverflow
Solution 3 - WpfbenPearceView Answer on Stackoverflow
Solution 4 - WpfDavid HollinsheadView Answer on Stackoverflow