如何在 WPF 中设置标签的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25212480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the background color of a label in WPF?
提问by Michel Keijzers
I have a label but setting the Background property not seem to do anything:
我有一个标签,但设置 Background 属性似乎没有做任何事情:
<Label Content="{Binding Name, Source={StaticResource LocStrings}}"
HorizontalAlignment="Left" Margin="4" Name="label2" Background="Blue"
VerticalAlignment="Top"/>
This does not show a blue background (while the property Background is recognized.
这不会显示蓝色背景(同时识别属性背景。
Also when using the Label.Background 'way' I do not see a blue background.
此外,当使用 Label.Background 'way' 时,我看不到蓝色背景。
Update:
更新:
I used the following minimalistic code:
我使用了以下简约代码:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="TEST" SizeToContent="WidthAndHeight">
<Grid>
<Label Content="TEXT TO TEST" Foreground="Green" Background="Orange"/>
</Grid>
</Window>
What I see is the tekst in green but without any orange background.
我看到的是绿色的 tekst,但没有任何橙色背景。
回答by Stígandr
<Label Content="{Binding Name, Source={StaticResource LocStrings}}"
HorizontalAlignment="Stretch" Margin="4" Name="label2" Background="Blue"
VerticalAlignment="Top"/>
Have you tried to just enter some text in the Content and checked the binding output, maybe there is something wrong with your binding. Because it works just fine here. Note that I set HorizontalAlignment="Stretch"instead of left, which will make the label use all the horizontal avaliable space. If you don't have anything bound your label will be invisible in your case above, you may use this in combination with the output to figure out what's likely wrong with your binding as stated by others, like Sriram Sakthivel and PoweredByOrange. For helping you with that, we need a bit more information :)
您是否尝试在内容中输入一些文本并检查绑定输出,也许您的绑定有问题。因为它在这里工作得很好。请注意,我设置了HorizontalAlignment="Stretch"而不是 left,这将使标签使用所有水平可用空间。如果您没有绑定任何东西,您的标签在上述情况下将不可见,您可以将其与输出结合使用,以找出其他人(如 Sriram Sakthivel 和 PoweredByOrange)所述的绑定可能有什么问题。为了帮助您,我们需要更多信息:)
Hope it helps,
希望能帮助到你,
Cheers,
干杯,
Stian
斯蒂安
回答by pushpraj
Since you are binding to a string value so using a TextBlockinstead of Labelis worth here. A content model of Label may not be required in this scenario.
由于您绑定到一个字符串值,因此在这里使用TextBlock代替Label是值得的。在这种情况下可能不需要 Label 的内容模型。
here is an example
这是一个例子
<TextBlock Text="{Binding Name, Source={StaticResource LocStrings}}"
HorizontalAlignment="Left" Margin="4" Name="label2" Background="Blue"
VerticalAlignment="Top"/>
some other benefits of displaying text in a TextBlock instead of a Label
在 TextBlock 而不是 Label 中显示文本的其他一些好处
Unlike a Label a Textblock is derived directly from FrameworkElement instead of deriving from a Control thus making it lightweight.
与 Label 不同,Textblock 直接从 FrameworkElement 派生,而不是从 Control 派生,从而使其轻量级。
A Label follows content model so the appearance may get affected by the content and its type and / or any style or template defined for the same.
标签遵循内容模型,因此外观可能会受到内容及其类型和/或为其定义的任何样式或模板的影响。
read here for more Differences between Label and TextBlock
阅读此处了解更多Label 和 TextBlock 之间的差异

