向文本块添加边框

是否可以向文本块添加边框。我需要它被添加到 setter 属性下面的代码:

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

You need to wrap your TextBlock in a Border. Example:

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

Of course, you can set these properties (BorderThickness, BorderBrush) through styles as well:

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

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

See this link for more on the differences between a TextBlock and other Controls