[英]Change background colour of TextBlock in C#
Currently porting an application to Windows Phone 7 I've encountered a problem that should be trivial 当前将应用程序移植到Windows Phone 7时,我遇到了一个小问题
All I want is change the background colour of a TextBlock. 我想要的只是更改TextBlock的背景颜色。 Using the WYSIWYG I can easily create a TextBlock, change the foreground and background colour.
使用所见即所得,我可以轻松创建TextBlock,更改前景色和背景色。 So for a TextBlock using white text on black background I would use:
因此,对于在黑色背景上使用白色文本的TextBlock,我将使用:
<TextBox Height="148" HorizontalAlignment="Left" Margin="106,0,0,0" Name="textBox1" Text="TextBox" VerticalAlignment="Top" Width="460" Background="Black" BorderBrush="Black" Foreground="White" />
But I need to do it in code (C#) and the Background doesn't appear to be a property of TextBlock. 但是我需要在代码(C#)中执行此操作,并且Background似乎不是TextBlock的属性。 How come it's something you can do using the resource editor, but not in code?
您怎么能使用资源编辑器来完成某件事,而不能使用代码呢?
I've found various similar questions, but no definitive answer. 我发现了各种类似的问题,但没有明确的答案。 In Microsoft documentation (.Net), TextBlock does appear to have a Background property
在Microsoft文档(.Net)中,TextBlock确实具有Background属性
Is there a way to do this in code without having to put the TextBlock inside a container (like Grid) which does have the Background property? 有没有一种方法可以在代码中执行此操作而不必将TextBlock放入具有Background属性的容器(如Grid)中? Thanks JY
谢谢JY
TextBlock
is not inherited from Control
, it doesn't have a Background
property. TextBlock
不是从Control
继承的,它没有Background
属性。 The code you are showing is a TextBox
not a TextBlock
. 您显示的代码是
TextBox
而不是TextBlock
。 TextBox
inherites from Control
and has a Background
property. TextBox
继承自Control
并具有Background
属性。 The simplest way is to wrap it with a Panel
, or you can create a custom control for it. 最简单的方法是用
Panel
包装它,或者您可以为其创建自定义控件。
Also, in silverilght sdk, you have a control called Label
and it is inherited from Control
. 另外,在silverilght sdk中,您有一个名为
Label
的控件,它是从Control
继承的。 You can probably get the source code from there and implement it in your project. 您可能可以从那里获取源代码并在您的项目中实现它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.