WPF: CornerRadius - no rounding of all corners
Using CornerRadius it is possible to create rounded corners very easy. Here is an example:
<Window x:Class=“CornerRadiusSample.Window1″ xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml” Title=“Window1″ Height=“300″ Width=“300″> <Grid> <Label Background=“Blue” Name=“TestLabel” Width=“100″ Height=“100″> <Label.Template> <ControlTemplate> <Border BorderThickness=“1″ Padding=“0,10,0,10″ Background=“{TemplateBinding Background}” BorderBrush=“{TemplateBinding BorderBrush}” CornerRadius=“15″/> </ControlTemplate> </Label.Template> </Label> </Grid> </Window>
The following screenshot shows the result:
Iin some cases not all corners should be rounded. This can be done if you specify the angle for all corners separately, shown as follows:
<Window x:Class=“CornerRadiusSample.Window1″ xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml” Title=“Window1″ Height=“300″ Width=“300″> <Grid> <Label Background=“Blue” Name=“TestLabel” Width=“100″ Height=“100″> <Label.Template> <ControlTemplate> <Border BorderThickness=“1″ Padding=“0,10,0,10″ Background=“{TemplateBinding Background}” BorderBrush=“{TemplateBinding BorderBrush}” CornerRadius=“0,15,0,15″/> </ControlTemplate> </Label.Template> </Label> </Grid> </Window>
And here is the new result:
Of course you can to the same with other controls, for example a ListBox or something like that.
May 9th, 2008 at 9:12 pm
Is it possible to do Binding on separate corners of the CornerRadius. I would like to have a slider that the top corners on the Border increase/decrease radius as the slider is moved. But I don’t want the bottom to corners to be affected.
May 9th, 2008 at 11:25 pm
[…] after posing this question over at .NET Blog it took me all of about 30 seconds to realize what I needed to do. Is it possible to do Binding on […]
May 13th, 2008 at 11:06 am
Joe, yes it is possible. You have to use a ValueConverter (see IValueConverter).