WPF 自定义按钮类实现

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

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)

<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.项目添加样式文件

<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.使用自定义按钮

<!-- 第四行 登录按钮 -->
<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}"/>
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值