Monthly Archives: December 2014

WPF textblock combine binding with static text

Instead of having say two text boxes in a horizontally aligned stack panel, one configured to display a static label and another configured to display dynamic text via binding as follows:

  <StackPanel Orientation="Horizontal">               
                <TextBlock Margin="10,0" Text="Licensed To:"/>
                <TextBlock Margin="10,0" Text="{Binding LicensedTo" />
            </StackPanel>            

WPF 4 + allows me to define a binding alongside static text using a single TextBlock.

The syntax for this is as follows:

  <StackPanel>               
                <TextBlock Margin="10,0">
                   Licensed To: <TextBlock Margin="10,0" Text="{Binding LicensedTo" />
                </TextBlock>
            </StackPanel>            

And if this is all you used the StackPanel for, it is no longer necessary, simplifying your visual tree.

Advertisements