WPF Add a Border to a TextBlock

Wpf

Wpf Problem Overview


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>

Wpf Solutions


Solution 1 - Wpf

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>

Solution 2 - Wpf

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBruieView Question on Stackoverflow
Solution 1 - WpfHeinziView Answer on Stackoverflow
Solution 2 - WpfRachelView Answer on Stackoverflow