WPF 用户控件 - 以编程方式圆角 [英] WPF User Control - Round corners programmatically

查看:72
本文介绍了WPF 用户控件 - 以编程方式圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个 WPF 问题...

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

推荐答案

您需要使用 Border 来提供圆角,因此您可以执行以下操作:

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>

然后向您的 UserControl 添加一个属性:

And then add a property to your UserControl:

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

它允许您从代码中设置边框的 CornerRadius.

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

这篇关于WPF 用户控件 - 以编程方式圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆