Wpf 将边框添加到我的文本块样式中

Wpf 将边框添加到我的文本块样式中,wpf,Wpf,我有这种风格: <Style x:Key="labelStyle" TargetType="TextBlock"> <Setter Property="Foreground" Value="White" /> <Setter Property="Background" Value="Gray" /> </Style> 我在图表SmartLabel上使用此样式,我想添加边框。 有什么建议吗?您可以覆盖textblock的模板。在其模

我有这种风格:

<Style x:Key="labelStyle" TargetType="TextBlock">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Background" Value="Gray" />
</Style>
我在图表SmartLabel上使用此样式,我想添加边框。
有什么建议吗?

您可以覆盖textblock的模板。在其模板中放置一个边框,并为borders子项放置一个textblock。

将textblock包装在边框内:

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

您也可以在边框上使用其他属性,如BorderBrush、Thickness等

我可以使用代码示例吗?请查看答案这是我的第一个建议。但是TextBlock没有模板,那么我有什么选择?@berrywer在边框内添加TextBlock。然后呢?我需要使用TargetType=TextBlock的样式