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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s