WPF按钮Radius化

在WPF中,可以通过以下几种方式为按钮添加圆角(Radius化):

方法一:通过自定义按钮模板

在XAML中,可以通过自定义按钮的ControlTemplate来设置圆角。具体步骤如下:

  1. 定义一个ControlTemplate,并在其中使用Border控件。
  2. Border控件设置CornerRadius属性。

示例代码:

xml 复制代码
<Button Content="圆角按钮" Width="120" Height="40">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border CornerRadius="10" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

方法二:使用全局样式

如果需要为多个按钮设置统一的圆角样式,可以定义一个全局样式。

示例代码:

xml 复制代码
<Window.Resources>
    <Style x:Key="RoundButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border CornerRadius="10" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Button Content="圆角按钮" Width="120" Height="40" Style="{StaticResource RoundButtonStyle}"/>

方法三:直接在Border控件中设置

如果按钮的内容是通过Border控件包装的,可以直接在Border控件中设置CornerRadius

示例代码:

xml 复制代码
<Button Content="圆角按钮" Width="120" Height="40">
    <Button.Resources>
        <Style TargetType="{x:Type Border}">
            <Setter Property="CornerRadius" Value="10"/>
        </Style>
    </Button.Resources>
</Button>

方法四:通过代码动态设置

如果需要在代码中动态创建按钮并设置圆角,可以通过以下方式:

csharp 复制代码
Button myButton = new Button
{
    Content = "动态圆角按钮",
    Width = 120,
    Height = 40
};

var buttonTemplate = new ControlTemplate(typeof(Button))
{
    VisualTree = new FrameworkElementFactory(typeof(Border))
    {
        SetValue(Border.CornerRadiusProperty, new CornerRadius(10)),
        SetValue(Border.BackgroundProperty, Brushes.LightBlue),
        SetValue(Border.ChildProperty, new FrameworkElementFactory(typeof(ContentPresenter))
        {
            SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Center),
            SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center)
        })
    }
};

myButton.Template = buttonTemplate;

以上方法可以根据实际需求选择使用,实现按钮的圆角效果。

相关推荐
爱吃煎蛋的小新8 小时前
WPF入门 #1 WPF布局基础
笔记·学习·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘8 小时前
WPF学习(二)
学习·wpf
BearHan1 天前
非常'肤浅'的理解MVVM
wpf
ou.cs1 天前
wpf 控件开发中,OnApplyTemplate 和 OnContentRendered区别
c#·.net·wpf
界面开发小八哥2 天前
界面组件DevExpress WPF中文教程:Grid - 节点(Nodes)概述
.net·wpf·界面控件·devexpress·ui开发
ou.cs2 天前
wpf 队列(Queue)在视觉树迭代查找中的作用分析
wpf
code bean2 天前
【WPF】WPF 中 `DisplayMemberPath` 与 `SelectedValuePath` 的深入理解与实战应用
windows·wpf
Magnum Lehar3 天前
wpf3d游戏引擎EditorColors.xaml实现
ui·游戏引擎·wpf
沉到海底去吧Go3 天前
【图片识别改名】如何批量识别大量图片的文字并重命名图片,基于WPF和京东OCR识别接口的实现方案
ocr·wpf·图片文字识别·图片识别重命名·图片区域识别改名·图片批量识别文字