问题描述
我想知道是否存在不使用转换器就可以使用Multibinding的情况-以及迫使我们使用转换器的局限性.
I would like to know if there are scenarios where you can use a Multibinding without a converter - and the limitations which force us to use a converter.
特别是我正在尝试将一个字符串与另一个以string.format样式的两个字符串绑定.
In particular I am trying to bind a string to another two strings in a string.format style.
推荐答案
在不使用转换器的情况下,使用MultiBinding
的最常见区域是当字符串格式连接两个单独的值时
The most common area you use a MultiBinding
without a converter is when you have a string format concatenating two individual values
举个例子:
要格式化具有第一",最后"部分的名称,并且您要根据语言环境对其进行格式化
To format Names that have First, Last part and you want to format it based on locale
<StackPanel>
<TextBlock x:Name="firstName"
Text="John" />
<TextBlock x:Name="lastName"
Text="Wayne" />
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding ElementName="firstName"
Path="Text" />
<Binding ElementName="lastName"
Path="Text" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
您确实看到了很多使用转换器的地方,因为使用MultiBinding
与Binding
相同,但是您将多个源值格式化为单个结果,而不是单个输入->单个输出.
You do see quite a lot of places you use a converter since using a MultiBinding
your doing the same as a Binding
but you have multiple source values formatted to a single result instead of single input -> single output.
您可以让Binding接受ConverterParameter
来提供另一个输入值,但是您有一些局限性,例如无法为其提供运行时绑定值,这使得MultiBinding
更适合要绑定的多个输入全部.
You can have a Binding take a ConverterParameter
to supply another input value however you have limitations like not being able to provide a runtime Bound value to it, which makes MultiBinding
more appropriate for multiple inputs where you want to bind all of them.
它可以归结为您的用例,如果您想提供基于自定义方式评估的不同输入类型的结果,则需要一个Converter(与Binding非常相似.请考虑一下区别在于1个输入可绑定值,而不是多个)
It boils down to your use-case, If you want to provide a result based on different input types that you evaluate in a custom-way, you need a Converter(pretty much similar to Binding. Just think of the difference as 1 input bind-able value against multiple)
这篇关于在WPF中使用多重绑定时是否需要使用转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!