121

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?

4
  • 1
    any code snippets/examples please? Commented Jul 27, 2010 at 6:35
  • I think some part of the question is missing, you are refering to binding above but I don't see any binding code in the question. Commented Jul 27, 2010 at 6:41
  • 1
    Somehow each time I have a question someone had the exactly same question previously :) Great! Commented Mar 24, 2012 at 9:29
  • 1
    A fourth way to do this would be to use this answer: stackoverflow.com/a/2552911/222134 Commented May 31, 2014 at 14:55

4 Answers 4

180

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>
Sign up to request clarification or add additional context in comments.

3 Comments

@tofutim That's because MultiBinding doesn't seem to have Resources. Use TextBlock.Resources instead...
Maybe you should add xmlns:sys="clr-namespace:System;assembly=mscorlib" to your solution ;-)
This code even works in 2024 with .NET MAUI :-) Thanks!
127

Or, combining the two answers above:

Define the namespace sys at the document head:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

and then:

<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.

1 Comment

It's needed to define the namespace sys at the document head: xmlns:sys="clr-namespace:System;assembly=mscorlib"
33

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">

3 Comments

<Binding Source="123" /> seems to pass DependencyProperty.UnsetValue
<Binding Source="123" /> passes the string "123" instead of the integer or double that I intended in my case.
In IValueConverter, the parameters are passed as object, which means you would need cast the value to the correct type, in a safe way. IValueConverter.Convert
9

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.