locked
How can I set the background color of a textblock RRS feed

  • Question

  • I have a few textblocks in a StackPanel...

    <StackPanel Orientation="Vertical" Grid.Row="1" Grid.Column="1">
    <TextBlock>Bob Smith</TextBlock>
    <TextBlock>Age : 31</TextBlock>
    <TextBlock TextWrapping="Wrap">Applications Integrator</TextBlock>
    </StackPanel>

    Is there any way I can set the background color of one of the textblocks, preferebly in a gradient?

    Thanks

    Tuesday, November 18, 2008 3:23 AM

Answers

  •  Hi,

    TextBlock does not have any background property. So You can wrap the text blocks inside a Border for workaround.

    <Border Background="AliceBlue"  Height="30" Width="100">
                <TextBlock > Hello There</TextBlock>
    </Border>

    Tuesday, November 18, 2008 4:14 AM

All replies

  •  Hi,

    TextBlock does not have any background property. So You can wrap the text blocks inside a Border for workaround.

    <Border Background="AliceBlue"  Height="30" Width="100">
                <TextBlock > Hello There</TextBlock>
    </Border>

    Tuesday, November 18, 2008 4:14 AM
  • That works perfectly!

    Thanks

    Tuesday, November 18, 2008 4:17 AM
  • Would there be any way to make this background color a gradient?
    The Border doesn't seem to have a FILL property.

     Thanks

    Tuesday, November 18, 2008 4:21 AM
  • Yes, you can easily fill a gradient in a Border's background through Blend or by Xaml; something like this:

    <Border Height="91" Width="126">
      <Border.Background>
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#FF000000"/>
        <GradientStop Color="#FFA32F2F" Offset="1"/>
       </LinearGradientBrush>
      </Border.Background>
     </Border>

    (Please 'Mark as Answer' if it helps you)

    Tuesday, November 18, 2008 5:20 AM