Search code examples
wpf

WPF Add a Border to a TextBlock


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>

Solution

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