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>
Borders in WPF works little differently. Border in XAML is its own control that can be applied to other controls or XAML elememts. To place a border around an element, WPF provides the Border element. Similar to other WPF elements, the Border has Width, Height, Background, and HorizontalAlignment and VerticalAlignment properties.
WPF TextBlock. A TextBlock control in WPF provides a lightweight control for displaying small amounts of flow content. This tutorial and code examples demonstrates how to use a WPF TextBlock control in a WPF app and set its font style, text formatting, alignment, text decorations and other properties in XAML and C#.
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
The Foreground property sets the foreground color of contents. This control does not have a Background property. The code snippet in Listing 1 creates a TextBlock control and sets the name, height, width, foreground and content of a TextBlock control. Unlike a TextBox control, the TextBlock does not have a default border around it.
No, 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With