在C#中更改TextBlock的背景颜色
当前将应用程序移植到 Windows Phone 7 我遇到了一个应该是微不足道的问题,
我想要的只是更改 TextBlock 的背景颜色。 使用所见即所得我可以轻松创建文本块,更改前景色和背景色。 因此,对于在黑色背景上使用白色文本的 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" />
但我需要在代码(C#)中执行此操作,并且背景似乎不是 TextBlock 的属性。 为什么可以使用资源编辑器执行此操作,但不能在代码中执行?
我发现了各种类似的问题,但没有明确的答案。 在Microsoft文档(.Net)中,TextBlock确实具有Background属性
是否有一种方法可以在代码中执行此操作,而不必将TextBlock放入具有Background属性的容器(如Grid)中? 谢谢 杰宇
Currently porting an application to Windows Phone 7 I've encountered a problem that should be trivial
All I want is change the background colour of a TextBlock.
Using the WYSIWYG I can easily create a TextBlock, change the foreground and background colour.
So for a TextBlock using white text on black background I would use:
<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.
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
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?
Thanks
JY
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TextBlock
不是从Control
继承的,它没有Background
属性。您显示的代码是TextBox
而不是TextBlock
。TextBox
继承自Control
并具有Background
属性。最简单的方法是用Panel
包装它,或者您可以为其创建自定义控件。另外,在 silverilght sdk 中,您有一个名为
Label
的控件,它继承自Control
。您也许可以从那里获取源代码并在您的项目中实现它。TextBlock
is not inherited fromControl
, it doesn't have aBackground
property. The code you are showing is aTextBox
not aTextBlock
.TextBox
inherites fromControl
and has aBackground
property. The simplest way is to wrap it with aPanel
, or you can create a custom control for it.Also, in silverilght sdk, you have a control called
Label
and it is inherited fromControl
. You can probably get the source code from there and implement it in your project.