Random programming things I'd want to remember

Monday, April 30, 2012

How to add Run control dynamically




textBlock.Inlines.Clear();
textBlock.Inlines.Add(new Run() { Text = Answer.Tag.ToString() });



Wednesday, April 11, 2012

How to display more than 1 text fragment on the same line using XAML

If using TextBlock, use Text.Inlines property. That can help do things like this:

<TextBlock TextWrapping="Wrap">
    <TextBlock.Inlines>
        <Run FontWeight="Bold" Text="And" />
        <Run Text=" then..." />
    </TextBlock.Inlines>
</TextBlock>


Which will look like:
And then...

If there is a need to use different types of controls, then one could use the Grid control like so:

<Grid x:Name="grid1>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="300" />
    <ColumnDefinition Width="*"/>
  </Grid.ColumnDefinitions>
  
  <TextBlock x:Name="txtBlock1" Width="260" Grid.Column="0" Text="Text" />
  <TextBox x:Name="txtBox1" Text="a" Width="120" Grid.Column="1"></TextBox>
</Grid>