Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF label styling

I have the following style:

<Style x:Key="WhiteStyle" TargetType="{x:Type Label}">               
    <Setter Property="BorderBrush" Value="White"/>
    <Setter Property="BorderThickness" Value="2"/>    
</Style>

However, I would like to add the property CornerRadius and modify the value. Unfortunately, the XAML error says a Label does not have a CornerRadius property. My question, How must I modify this XAML?

Thanks,

like image 502
DoubleDunk Avatar asked Jul 12 '13 18:07

DoubleDunk


People also ask

How do I apply styles in XAML?

Apply a style implicitly You can change the default appearance by setting properties, such as FontSize and FontFamily, on each TextBlock element directly. However, if you want your TextBlock elements to share some properties, you can create a Style in the Resources section of your XAML file, as shown here.

What is style in WPF?

Styles provide us the flexibility to set some properties of an object and reuse these specific settings across multiple objects for a consistent look. In styles, you can set only the existing properties of an object such as Height, Width, Font size, etc. Only default behavior of a control can be specified.


1 Answers

The error is correct, you cannot set a corner radius on a Label.

What you can do is wrap the Label with a Border and apply your style to that to get the desired look.

EDIT:

The Style Resource:

<Style x:Key="MyBorderStyle" TargetType="Border">
      <Setter Property="BorderBrush" Value="White" />
      <Setter Property="BorderThickness" Value="2" />
      <Setter Property="CornerRadius" Value="3" />
</Style>

The border wrapped label:

<Border Style="{StaticResource MyBorderStyle}">
    <Label Content="My Label" />
</Border>
like image 94
TrueEddie Avatar answered Sep 20 '22 21:09

TrueEddie