如何在多绑定中传递1绑定的常量值?

我有一个多绑定喜欢

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

60952 次浏览

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.

定义 XAML 根目录中的 sys名称空间以指向 mscolib 中的 System,应该可以执行以下操作:

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

我不太明白这个问题,但有两个选择:

将行 <Binding Source="123" />放入您的多重绑定中,会将123作为绑定值传递给您的转换器。

ConverterParameter="123"放入多重绑定:

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

或者,将上述两个答案结合起来:

在文档头部定义名称空间系统:

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

然后:

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

它提供了正确的类型,而没有使用参考资料组装。

我不是说这是一个特别好的答案,但这里有另一种方法:

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