WPF User Control - Round corners programmatically

你。 提交于 2019-12-04 21:10:55

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

<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);

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

check this, Hope this helps!!

<Button x:Name="bbb"> b </Button>

var r=bbb.Template.FindName("border",bbb);
((Border)r).CornerRadius = new CornerRadius(40);

Call outside the constructor, maybe on Loaded event.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!