Wpf-自定义控件波纹Button

使用用户控件,继承Button

前端代码

XML 复制代码
<Button x:Class="WpfApp1.SuperButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        d:DesignHeight="450" d:DesignWidth="800"
        mc:Ignorable="d">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Border BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="5">
                <Border.Style>
                    <Style TargetType="Border">
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Effect">
                                    <Setter.Value>
                                        <DropShadowEffect Direction="-90" Opacity="0.5"
                                                          ShadowDepth="1" Color="Gray" />
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="False">
                                <Setter Property="Effect">
                                    <Setter.Value>
                                        <DropShadowEffect Direction="0" Opacity="0"
                                                          ShadowDepth="0" />
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <Grid Background="{TemplateBinding Background}"
                      ClipToBounds="True"
                      MouseLeftButtonDown="Grid_MouseLeftButtonDown">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      Content="{TemplateBinding Content}" />
                    <Path x:Name="MyPath" Fill="Black">
                        <Path.Data>
                            <EllipseGeometry x:Name="MyEll" RadiusY="{Binding RelativeSource={RelativeSource Mode=Self}, Path=RadiusX}" />
                        </Path.Data>
                    </Path>
                </Grid>
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

后端代码

cs 复制代码
    /// <summary>
    /// SuperButton.xaml 的交互逻辑
    /// </summary>
    public partial class SuperButton : Button
    {
        public SuperButton()
        {
            InitializeComponent();
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var circle = Template.FindName("MyEll", this) as EllipseGeometry;
            circle.Center = Mouse.GetPosition(this);
            var path = Template.FindName("MyPath", this) as Path;
            DoubleAnimation animation1 = new DoubleAnimation()
            {
                From = 0,
                To = 150,
                Duration = new Duration(TimeSpan.FromSeconds(0.3)),
            };
            circle.BeginAnimation(EllipseGeometry.RadiusXProperty, animation1);
            DoubleAnimation animation2 = new DoubleAnimation()
            {
                From = 0.3,
                To = 0,
                Duration = new Duration(TimeSpan.FromSeconds(0.3)),
            };
            path.BeginAnimation(Path.OpacityProperty, animation2);

        }
    }
相关推荐
楼田莉子3 分钟前
C++20新特性:Range库
开发语言·c++·后端·学习·c++20
念恒123066 分钟前
Python 函数完全指南:定义与调用
开发语言·python
曹牧8 分钟前
Java:Unix时间戳
java·开发语言
会编程的土豆15 分钟前
Go 里的 error 接口 + 假 nil(超级重点)
开发语言·后端·golang
愿天垂怜15 分钟前
【C++脚手架】ffmpeg 库的介绍与使用
linux·服务器·开发语言·c++·ide·git·ffmpeg
并不喜欢吃鱼16 分钟前
从零开始 C++-----十一【C++ 数据结构】红黑树全解析:从定义到工程实现(一文搞定,十分详细)
开发语言·数据结构·c++
不会C语言的男孩17 分钟前
C++ Primer Plus 第7章:函数——C++的编程模块
开发语言·c++
方也_arkling18 分钟前
【Java-Day09】继承
java·开发语言
迈巴赫车主19 分钟前
蓝桥杯21247弹跳鞋java
java·开发语言·数据结构·算法·职场和发展·蓝桥杯
SoftLipaRZC25 分钟前
C语言数据在内存中的存储:整型与浮点型的秘密
c语言·开发语言