wpf 如何在多绑定中为 1 绑定传递常量值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3340821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pass a constant value for 1 binding in multi-binding?
提问by Nam G VU
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?
我想将一个固定值(例如“123”)传递给上述两个绑定之一。我如何使用 XAML 做到这一点?
回答by 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.
如果您的值只是 a string
,您可以在Source
绑定的属性中将其指定为常量。如果是任何其他原始数据类型,则需要定义一个静态资源并引用它。
Define the sys
namespace in the root of the XAML to point to System
in mscorlib, and the following should work:
sys
在 XAML 的根中定义命名空间以System
在 mscorlib 中指向,并且以下内容应该起作用:
<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>
回答by 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.
它提供了正确的类型而没有资源杂乱。
回答by benPearce
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.
将该行<Binding Source="123" />
放入您的多重绑定中,会将 123 作为绑定值传递给您的转换器。
Put ConverterParameter="123"
in your MultiBinding:
放入ConverterParameter="123"
你的多重绑定:
<MultiBinding Converter="{StaticResource conv}" ConverterParameter="123">
<MultiBinding Converter="{StaticResource conv}" ConverterParameter="123">
回答by David Hollinshead
I'm not saying this an especially good answer but here is another approach:
我并不是说这是一个特别好的答案,但这是另一种方法:
<Binding Path="DoesNotExist" FallbackValue="123" />