WPF向TextBlock添加边框

Bru*_*uie 64 wpf

是否可以为文本块添加边框.我需要将它添加到代码下面的setter属性中:

<Style x:Key="notCalled" TargetType="{x:Type TextBlock}">
    <Setter Property="Margin" Value="2,2,2,2" />
    <Setter Property="Background" Value="Transparent" />
</Style>
Run Code Online (Sandbox Code Playgroud)

Hei*_*nzi 117

不,您需要将TextBlock包装在Border中.例:

<Border BorderThickness="1" BorderBrush="Black">
    <TextBlock ... />
</Border>
Run Code Online (Sandbox Code Playgroud)

当然,您也可以通过样式设置这些属性(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>
Run Code Online (Sandbox Code Playgroud)


Rac*_*hel 26

TextBlock实际上并不从Control继承,因此它没有通常与Control关联的属性.在样式中添加边框的最佳选择是将TextBlock替换为Label

有关TextBlock与其他控件之间差异的更多信息,请参阅此链接

  • 很好的答案,我更喜欢这个必须引入另一个控件/边框.哇自2010年以来仍然有效:) (3认同)