tags:

views:

40

answers:

1

Does WPF support multiple binding expressions in one statement? Something along the lines of the following:

 <TextBlock Text="{Binding Path=OrderID} shipped on {Binding Path=OrderDate}"/>

I'm guessing that it does but I think I just don't have the correct syntax.

+3  A: 

You have to use a MultiBinding with the StringFormat feature. Look at the docs for more info

<TextBox>
  <TextBox.Text>
    <MultiBinding StringFormat="{}{0} shipped on {1:D}">
      <Binding Path="OrderID" />
      <Binding Path="OrderDate"/>
    </MultiBinding>
  </TextBox.Text>
</TextBox>

To add support for forrmating specific sections of the textblock, use Inlines like so.

<Textblock>
   <Run FontWeight="Bold" Text="{Binding OrderID}"/>
   <Run Text="shipped on "/>
   <Run FontStyle="Italic" Text="{Binding OrderDate}"/>
</Textblock>
Mike Brown
Awesome! Just 1 question. Is there a way to apply styles to parts of the text, such as making the OrderID and Date Bold when using MultiBinding or would I need to split up the parts into multiple TextBlocks
Abhijeet Patel
In order to do what you want use the Inlines initialization. I've updated the response to show.
Mike Brown