How to set background color on TextBlock

Steve Wood 56 Reputation points
2020-02-24T14:36:13.843+00:00

How can the background color on a TextBlock control be changed?

Universal Windows Platform (UWP)
No comments
{count} votes

3 answers

Sort by: Most helpful
  1. macintoshpro 36 Reputation points
    2020-02-24T15:37:11.557+00:00

    TextBlock don't have background property, but you can put it inside a Grid and set background for grid
    3391-msqa.png


  2. Steve Wood 56 Reputation points
    2020-02-25T00:44:51.297+00:00

    Ah...right...a child control inherits some of the visual attributes of the parent. So, I can put the TextBlock in one of the panels such as a Canvas or ItemsPanel and set the background on the panel. Sure is humongous overhead just to set the background of a control.

    No comments

  3. Roy Li - MSFT 26,556 Reputation points Microsoft Employee
    2020-02-25T01:10:29.213+00:00

    Hello,

    TextBlock derives from FrameworkElement and there is no background property for FrameworkElement. So there is no background property for the Textblock class.
    Generally, we will provide the background effect with an additional element looks like a container for the TextBlock. For example, we could use Border or Grid like @macintoshpro mentioned in his reply.

    If you are using border, it looks like this:

    <Border Background="Red">  
      <TextBlock/>  
    </Border>  
    

    Thank you.