Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Add a Border to a TextBlock

Tags:

wpf

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> 
like image 936
Bruie Avatar asked Oct 19 '10 16:10

Bruie


People also ask

How do I put a border around an element in WPF?

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.

What is text block in WPF?

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#.

Is it possible to add a border to a textblock?

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

How do I set the foreground and background of a textblock?

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.


2 Answers

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> 
like image 54
Heinzi Avatar answered Oct 15 '22 07:10

Heinzi


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

like image 23
Rachel Avatar answered Oct 15 '22 09:10

Rachel