Do you have to use a converter when using Multibinding in WPF?

℡╲_俬逩灬. 提交于 2019-12-10 19:20:37

问题


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.

In particular I am trying to bind a string to another two strings in a string.format style.


回答1:


The most common area you use a MultiBinding without a converter is when you have a string format concatenating two individual values

say for example:

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>

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.

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.

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)



来源:https://stackoverflow.com/questions/15759308/do-you-have-to-use-a-converter-when-using-multibinding-in-wpf

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