سؤال

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.

هل كانت مفيدة؟

المحلول

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)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top