How can the background color on a TextBlock control be changed?
How can the background color on a TextBlock control be changed?
TextBlock don't have background property, but you can put it inside a Grid and set background for grid
@macintoshpro note that you do no longer need to insert an image to show HTML code. We now support HTML code in posts: HTML code in your posts
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.
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.
I decided on using a border, but I'm doing it at runtime so it looks like this:
Border thisBorder = new Border();
TextBlock thisTextBlock = new TextBlock();
thisBorder.Child = thisTextBlock;
A Border control is the way to go if we need a container for a single control because a Border can only have one Child; thus, doesn't have the overhead of the Children property which is a list of UIElements that the other containers have.
Sure, glad to hear that it meets your requirements. If this helps you, please accept it as answer. It might help other people with the same problem.
11 people are following this question.
How to write and read multiple types of data using Data Writer and Data Reader
Consuming UWP mail api from WPF Desktop Bridge App
Get Network usage information in UWP apps
How to Get/Set Cookies in Windows.Web.Http API
Switch the ListView Datatemplate from one data template to another data template?