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}"/>

相关推荐

  1. WPF 定义按钮实现

    2024-03-27 20:10:04       21 阅读
  2. C#WPF将变量或定义数据绑定到控件实例

    2024-03-27 20:10:04       19 阅读
  3. C#WPF定义控件-继承Button的圆角按钮

    2024-03-27 20:10:04       12 阅读
  4. 实现C++定义的String

    2024-03-27 20:10:04       21 阅读
  5. wpf 实现3D按钮

    2024-03-27 20:10:04       33 阅读
  6. 定义 按钮间,按钮边框滑动。

    2024-03-27 20:10:04       39 阅读
  7. WPF定义快捷命令

    2024-03-27 20:10:04       26 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-27 20:10:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-27 20:10:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-27 20:10:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-27 20:10:04       20 阅读

热门阅读

  1. 形式语言理论简介及应用

    2024-03-27 20:10:04       18 阅读
  2. std::ratio

    2024-03-27 20:10:04       19 阅读
  3. 一些常见的MySQL问题和答案

    2024-03-27 20:10:04       17 阅读
  4. Docker未授权访问漏洞

    2024-03-27 20:10:04       20 阅读
  5. 面试算法-119-用栈实现队列

    2024-03-27 20:10:04       14 阅读
  6. python 条件循环语句

    2024-03-27 20:10:04       16 阅读
  7. 【C++】每日一题 45 跳跃游戏

    2024-03-27 20:10:04       17 阅读
  8. MySQL中的聚簇索引与非聚簇索引

    2024-03-27 20:10:04       15 阅读
  9. PostgreSQL开发与实战(7.3)多版本并发控制3

    2024-03-27 20:10:04       19 阅读
  10. 【二】【设计模式】建造者模式

    2024-03-27 20:10:04       17 阅读