WPF 为 TextBlock 添加边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3970522/
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
WPF Add a Border to a TextBlock
提问by Bruie
Is it possible to add a border to a textblock. I need it to be added in the setter property below code:
是否可以为文本块添加边框。我需要将它添加到下面代码的 setter 属性中:
<Style x:Key="notCalled" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="2,2,2,2" />
<Setter Property="Background" Value="Transparent" />
</Style>
回答by Heinzi
No, you need to wrap your TextBlock in a Border. Example:
不,您需要将 TextBlock 包装在 Border 中。例子:
<Border BorderThickness="1" BorderBrush="Black">
<TextBlock ... />
</Border>
Of course, you can set these properties (BorderThickness
, BorderBrush
) through styles as well:
当然,您也可以通过样式设置这些属性 ( BorderThickness
, BorderBrush
):
<Style x:Key="notCalledBorder" TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="Black" />
</Style>
<Border Style="{StaticResource notCalledBorder}">
<TextBlock ... />
</Border>
回答by Rachel
A TextBlock does not actually inherit from Control so it does not have properties that you would generally associate with a Control. Your best bet for adding a border in a style is to replace the TextBlock with a Label
TextBlock 实际上并不从 Control 继承,因此它没有通常与 Control 关联的属性。在样式中添加边框的最佳选择是将 TextBlock 替换为 Label
See this linkfor more on the differences between a TextBlock and other Controls
有关TextBlock 和其他控件之间差异的更多信息,请参阅此链接