2010-10-19 98 views
45

是否可以向文本块添加边框?我需要将它添加到代码如下的setter属性中:WPF向文本块添加边框

<Style x:Key="notCalled" TargetType="{x:Type TextBlock}"> 
    <Setter Property="Margin" Value="2,2,2,2" /> 
    <Setter Property="Background" Value="Transparent" /> 
</Style> 
+2

使用文本框,而不是更换TextBlock的。 – 2016-04-15 03:24:01

回答

86

不,您需要将TextBlock包装在边框中。例如:

<Border BorderThickness="1" BorderBrush="Black"> 
    <TextBlock ... /> 
</Border> 

当然,你也可以设置这些属性(BorderThicknessBorderBrush)通过样式以及:

<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> 
18

一个TextBlock实际上并没有从Control继承所以它不具有的属性,你通常会与Control相关联。在样式添加边框最好的办法是用标签

this link更多关于一个TextBlock和其他控件之间的差异

+2

优秀的答案,我更喜欢这个引入另一个控件/边框。哇2010以来,仍然有效:) – usefulBee 2014-10-28 18:36:04