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

偶尔善良 提交于 2019-11-28 16:17:01
Noldorin

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>
Anders Kaplan

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.

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

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

<Binding Path="DoesNotExist" FallbackValue="123" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!