21

I found this example on MSDN that shows some ways to configure a textblock:

A lot of it seems to work for me, but this part fails:

textBlock.Background = Brushes.AntiqueWhite;

The "Background" part is underscored in red and Visual Studio says: "Windows.UI.Xaml.Controls.TextBlock does not contain a definition for Background".

I am perplexed.

Is this a recent change? Or did this get removed later?

wonea
  • 4,783
  • 17
  • 86
  • 139
micahhoover
  • 2,101
  • 8
  • 33
  • 53
  • 3
    Your MSDN link is not for the Windows Runtime version of `TextBlock`. The proper link is here: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.ui.xaml.controls.textblock.aspx – chue x Mar 14 '13 at 03:22

5 Answers5

39

If I remember right WinRT is based a lot on Silverlight, in whereas TextBlock derives from FrameworkElement and unlike in WPF, it doesn't have a Background property of its own.

A workaround would be to just provide the same effect with an additional element to act as a container and provide your background using Border or Grid with Background etc. Something like;

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

Or perhaps a Rectangle behind the TextBlock to provide the same thing if it's contained in say maybe a Grid Cell or the likes unless you wanted to set sizes on the Rectangle directly;

<Rectangle Fill="AntiqueWhite"/>
<TextBlock/>

Unfortunately I think this is your only current alternative. Hope this helps.

Chris W.
  • 22,835
  • 3
  • 60
  • 94
  • Whats if i want to set 'background color' for 5 **TextBlock** among 9 TextBlock?? – Moumit Nov 25 '13 at 09:33
  • @MoumitMondal Then I would suggest maybe making a `ContentControl` using the same ideas as above so you can apply it multiple places more easily :) – Chris W. Nov 25 '13 at 14:19
  • Thanks @Chris W ... I just began to learn `XAML` 2 days ago ... So `ContentControl` also is not clear to me ... but i will get it soon ...:D – Moumit Nov 26 '13 at 05:33
  • @MoumitMondal no worries, asking questions is a good way to learn, its what SO is made for. :) – Chris W. Nov 26 '13 at 14:47
  • @ChrisW. `TextBlock` does have `Background`, where you can specify a color, so that part of this answer is wrong. – vapcguy Oct 27 '16 at 14:36
  • @vapcguy no, it [doesn't](https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.textblock), I'm guessing you're thinking of WPF? – Chris W. Oct 27 '16 at 14:38
  • `TextBlock` IS a WPF control. Where else would this be an element? It's not valid in ASP .NET... And even though apparently MSDN doesn't list it there, it was valid for me in my WPF project. – vapcguy Oct 27 '16 at 14:44
  • @vapcguy haha, ok dude. Yes WPF does have TextBlock...as a templated control which is why Background property is available via the border in the style template of it. However, Silverlight, UWP / WinRT etc also have TextBlock as a framework element.....they are not the same framework amigo. Hence why you see no WPF tag on the question. :) – Chris W. Oct 27 '16 at 14:46
  • @ChrisW. Ah, ok, that explains it, and I admit I misread your answer. I was going off the link on the OP's question, which went to `TextBlock.FontSize`. One level up from there was `TextBlock` Properties: https://msdn.microsoft.com/en-us/library/system.windows.controls.textblock_properties that clearly includes `Background`. But you did admit as such, for WPF. Apologies. – vapcguy Oct 27 '16 at 14:50
  • @vapcguy no worries, I hear ya. That's why I made sure to include clarification in my answer "and unlike in WPF" but I see where you're coming from. Always check the tags when you come in off of a google search though to make sure it's what you think it is. – Chris W. Oct 27 '16 at 14:52
  • @ChrisW. Welll he didn't tag it as WPF, but the MSDN link the OP provided went to the XAML control that has WPF properties. To me, I thought he just forgot to tag it. But I understand your point. – vapcguy Oct 27 '16 at 14:55
  • 1
    @vapcguy ya that's a pretty common scenario as folks are transitioning from WPF to UWP and are still learning the differences themselves since their familiarity is WPF. So they make a quick google, forget to check the framework, and post whatever is relevant to their question they can find. Like I said, no worries man. Cheers – Chris W. Oct 27 '16 at 14:57
  • @ChrisW. And you were right again... I just looked at the tags and the comment under the original question saying the MSDN link he was going to for his scenario was wrong for his use. He had tagged with `winrt-xaml`. Had no idea there was a difference, especially when my familiarity is with WPF and didn't know there was another type of XAML. Learn something new everyday, I suppose. – vapcguy Oct 27 '16 at 15:00
  • 1
    @vapcguy Nah the XAML is usually really the same except for some minor nuances here and there in syntax and things available in the usage like attached properties etc (except for when you get into like xamarin xaml where things like StackPanel becomes StackLayout, etc) however the frameworks using them are many...but they all derive from XML, just like so does HTML, SVG, etc etc etc.. It's all good. – Chris W. Oct 27 '16 at 16:14
  • This doesn't work, when the text box is selected, it's background reverts to white. – Gavin Williams Jul 10 '18 at 10:33
  • @GavinWilliams Well amigo, the question wasn't pertaining to `TextBox` so your comment and down-vote aren't correct. This was pertaining to `TextBlock` (different control entirely) so if you want to edit a `TextBox` you'll need to edit the `Style` Template for `TextBox` and also edit the colors for the selected and focused states in its `VisualStateManager` but thanks for the attention to the differences. Cheers :) – Chris W. Jul 26 '18 at 15:42
  • @Chris W. Ok, sorry for that. Apparently I can't change my vote. Voting is locked. – Gavin Williams Jul 27 '18 at 23:27
  • @GavinWilliams no worries, hope you found the remedy for your original issue. If not let me know if you ask in a separate question and we'll get you sorted. Cheers. – Chris W. Jul 29 '18 at 18:19
1

This sets the background to antique white. Furthermore the Height and width of the grid is bound to that of the TextBlock, so you don't have to manually set the size of the grid.

        <Grid Background="AntiqueWhite" Height="{Binding ActualHeight , ElementName=textBlock1}" Width="{Binding ActualHeight , ElementName=textBlock1}">
         <TextBlock x:Name="textBlock1" Text="Text" />
        </Grid>
nedR
  • 605
  • 1
  • 7
  • 14
1

To set the Background Colour for the TextBlock

TextblockName.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF202B49"));
0

In UWP, a lot of the controls have been simplified (probably to reduce their memory footprint) and do not have certain properties typical of their WPF brethren.

Instead of setting properties like .Background on the UWP element in question (as some answers already mention) it can be placed nested within a <Grid></Grid> element, but that is not recommended unless you want the properties you are assigning to the Grid to be shared among multiple child elements. If you want to apply a background (or other property not found in a UWP control) to a TextBlock or similar, what you should be doing is wrapping it in a <Border></Border> element, which is the recommended approach when you're only trying to customize a single element, and is a much lighter approach than wrapping it in a <Grid />, both in terms of layout computation and memory overhead.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
-1

Also you can use following code for dynamic changes. (Only for WPF)

textBlock.Background = new SolidColorBrush(Colors.AntiqueWhite);
Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31
  • Works fine for me in WPF-land. OP was asking about the XAML control `TextBlock`, as the MSDN link went to `TextBlock.FontSize`. One level up from there is `TextBlock` Properties: https://msdn.microsoft.com/en-us/library/system.windows.controls.textblock_properties(v=vs.110).aspx which clearly lists `Background` as a property. Those in the Silverlight/WinRT realm might not have it. I guess that's the reason for the downvotes. – vapcguy Oct 27 '16 at 14:53
  • If it's only for WPF then it's not related to the question. – Gavin Williams Jul 10 '18 at 10:30