What's the shorter xaml syntax for Multibinding using StringFormat with multiple bindings?
Asked Answered
F

1

8

for a single binding, we use:

<TextBlock>
  <TextBlock.Text>
    <MultiBinding StringFormat="{}{0}">
      <Binding Path=EmployeeName/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

or a shorter syntax:

<TextBlock 
 Text="{MultiBinding StringFormat=\{0\}, Bindings={Binding Path=EmployeeName}}"/>

Now, if you have multibinding:

<TextBlock>
  <TextBlock.Text>
    <MultiBinding StringFormat="{}{0}, {2}">
      <Binding Path="EmployeeName"/>
      <Binding Path="Age"/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

I was wondering, what would be its shorter syntax?

<TextBlock 
 Text="{MultiBinding StringFormat=\{0\}, Bindings={Binding ??????}"/>
Fara answered 7/12, 2009 at 16:15 Comment(0)
N
5

According to MSDN, your second example ("shorter syntax using MultiBinding with a single Binding") shouldn't work, neither in .net 3.5 nor in .net 4.0:

Note:

MultiBinding and PriorityBinding do not support a XAML extension syntax (despite sharing the same BindingBase class, which actually implements the XAML behavior for Binding).

So, if it works for you, that's by accident, and it's not supported behavior.


PS: You don't need to use MultiBinding for a single binding. The following should suffice:

<TextBlock>
    <TextBlock.Text>
        <Binding Path="EmployeeName" />
    </TextBlock.Text>
</TextBlock>

or

<TextBlock Text="{Binding Path=EmployeeName}"/>

or even shorter

<TextBlock Text="{Binding EmployeeName}"/>
Nonintervention answered 7/12, 2009 at 16:26 Comment(2)
Yes, it could be a non expected behavior. The shorter syntax I presented was generated by Visual Studio 2008 when pasting a WPF element. Since I didn't code it, I was thinking if it was possible to do this shorter version for multiple binding paths. Thank you Heinzi!Fara
oh such a shame, it would be really cool. Image something liike : AutomationProperties.Name="{MultiBinding.... where i want to use 2 static resources concatenated. now I have to defines a stile the the element in order to use multibinding etc. :(Sievert

© 2022 - 2024 — McMap. All rights reserved.