views:

478

answers:

3

Another WPF question...

<UserControl x:Class="TKEApp.Components.UserControls.ButtonControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <Grid Background="Black">
        <TextBlock Foreground="White" Background="Brown" Name="lblCaption" TextAlignment="Center"></TextBlock>
    </Grid>
</UserControl>

Somwhere in the application code I have an instance of this control and I need to make it's corners rounded programmatically. Is this possible?

+1  A: 

You need to use a Border to provide rounded corners, so you could do something like this:

<UserControl x:Class="TKEApp.Components.UserControls.ButtonControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <Border x:Name="border" Background="Black">
        <TextBlock Foreground="White" Background="Brown" Name="lblCaption" TextAlignment="Center"></TextBlock>
    </Border>
</UserControl>

And then add a property to your UserControl:

public int BorderRadius
{
    get { return border.CornerRadius; }
    set { border.CornerRadius = value; }
}

Which allows you to set the border's CornerRadius from code.

Groky
I need to do it programmatically, C# code, not XAML, and also I would like to use another solution other than surrounding it with border. But thanks anyway.
morsanu
Ah sorry, I didn't notice the programatically part of it. I don't think there's any way to make rounded corners other than using a border. I would ask though, *why* you're doing this? Are you just trying to change the appearance of a button control? If so there are better ways to do it than re-creating a button using a UserControl.
Groky
Updated the answer to allow you to do what you're asking. You still need a border though. But I think you should take a look at WPF's theming features before taking this route.
Groky
I need to do it for a visual designer. This user control is just a simple one, for example sake.
morsanu
+1  A: 
<UserControl x:Class="TKEApp.Components.UserControls.ButtonControl" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Transparent"> 
<Border x:Name="border"  Background="Black" BorderThickness="5" BorderBrush="Yellow"  > 
    <TextBlock Foreground="White" Background="Brown" Name="lblCaption" TextAlignment="Center"></TextBlock> 
</Border> 

First find out the user control using the FindName Method and

    Border brd=usercontrol.FindName("border") as Border;brd.CornerRadius=new CornerRadius(5);
Kishore Kumar
+1  A: 

you can also use RadiusX and RadiusY of Rectangle to create Rounded corners.

check this, Hope this helps!!

viky