开发者

WPF Add a Border to a TextBlock

开发者 https://www.devze.com 2023-01-20 16:24 出处:网络
Is it possible to add a border to a textblock. I need it to be added in the setter property below code:

Is it possible to add a border to a textblock. I need it to be added in the setter property below code:

<Style x:Key="notCalled" TargetType="{x:Type TextBlock}">
    <Setter Property="Margin" Value="2,2,2,2" />
  开发者_运维问答  <Setter Property="Background" Value="Transparent" />
</Style>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消