WPF 自定义按钮类实现

1.创建自定义按钮类 (CustomButton.cs)

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WPF_LoginUI
{
    // 自定义按钮类
    public class CustomButton : Button
    {
        // propdp
        // 依赖属性  CustomButtomStyles.xaml 绑定相关 ButtonCornerRadius 属性 ==> MainWindow.xaml 使用

        // 自定义属性 圆角半径 
        public CornerRadius ButtonCornerRadius
        {
            get { return (CornerRadius)GetValue(ButtonCornerRadiusProperty); }
            set { SetValue(ButtonCornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty ButtonCornerRadiusProperty =
            DependencyProperty.Register("ButtonCornerRadius", typeof(CornerRadius), typeof(CustomButton));

        // 自定义属性 鼠标掠过颜色
        public Brush BackgroundHover
        {
            get { return (Brush)GetValue(BackgroundHoverProperty); }
            set { SetValue(BackgroundHoverProperty, value); }
        }
        public static readonly DependencyProperty BackgroundHoverProperty =
            DependencyProperty.Register("BackgroundHoverProperty", typeof(Brush), typeof(CustomButton));


        // 自定义属性 鼠标按下颜色
        public Brush BackgroundPressed
        {
            get { return (Brush)GetValue(BackgroundPressedProperty); }
            set { SetValue(BackgroundPressedProperty, value); }
        }
        public static readonly DependencyProperty BackgroundPressedProperty =
            DependencyProperty.Register("BackgroundPressed", typeof(Brush), typeof(CustomButton));
    }
}

2.创建自定义按钮 样式xaml文件(CustomButtomStyles.xaml)

xml 复制代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         
                    xmlns:bb="clr-namespace:WPF_LoginUI">
    <!--使用关联的类 -->
    <Style TargetType="{x:Type bb:CustomButton}">
        <Setter Property="Template">
            <Setter.Value>
                <!--关联自定义类-->
                <ControlTemplate TargetType="{x:Type bb:CustomButton}">
                    <!-- 外框相关属性 Background 绑定既有属性 CornerRadius 绑定自定义属性-->
                    <Border x:Name="buttonBorder" 
                            Background="{TemplateBinding Background}" 
                            CornerRadius="{TemplateBinding ButtonCornerRadius}">
                            <!-- 中间文本框 绑定按钮自带属性 -->
                            <TextBlock Text="{TemplateBinding Content}" 
                                   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                   VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <!--触发器-->
                    <ControlTemplate.Triggers>
                        <!-- 触发器 鼠标掠过-->
                        <Trigger Property="IsMouseOver" Value="True">
                            <!-- 发生触发 修改Border的背景属性 -->
                            <Setter TargetName="buttonBorder" Property="Background" 
                                    Value="{Binding BackgroundHover, RelativeSource={RelativeSource TemplatedParent}}"></Setter>
                        </Trigger>
                        <!-- 触发器 鼠标按下-->
                        <Trigger Property="IsPressed" Value="True">
                            <!-- 发生触发 修改Border的背景属性 -->
                            <Setter TargetName="buttonBorder" Property="Background" 
                                    Value="{Binding BackgroundPressed, RelativeSource={RelativeSource TemplatedParent}}"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>

        </Setter>
    </Style>
</ResourceDictionary>

3.项目添加样式文件

xml 复制代码
<Application x:Class="WPF_LoginUI.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPF_LoginUI"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <!--添加自定义按钮样式 字典文件-->
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="CustomButtomStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

4.使用自定义按钮

xml 复制代码
<!-- 第四行 登录按钮 -->
<local:CustomButton x:Name="BtnLogin" Background="#3C7FF8" Grid.Row="3" Grid.Column="0" 
                    Foreground="Wheat" 
                    BackgroundHover="red"
                    BackgroundPressed="Green"
                    FontSize="20"
                    ButtonCornerRadius="5"
                    Content="登录" Grid.ColumnSpan="2" 
                    HorizontalContentAlignment="Center"
                    VerticalContentAlignment="Center"
        Command="{Binding LoginAction}"/>
相关推荐
wangnaisheng16 小时前
【WPF】Opacity 属性的使用
wpf
姬激薄21 小时前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)21 小时前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心1 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo2 天前
9.1.领域驱动设计
wpf
大道随心2 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠2 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go2 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet3 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf
冰茶_3 天前
WPF之绑定模式深入
学习·microsoft·微软·c#·wpf·绑定模式