Wpf-自定义控件波纹Button

使用用户控件,继承Button

前端代码

<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>

后端代码 

    /// <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);

        }
    }

相关推荐

  1. Wpf-定义波纹Button

    2024-03-24 07:26:01       20 阅读
  2. C#WPF定义-继承Button的圆角按钮

    2024-03-24 07:26:01       12 阅读
  3. Wpf-定义图标Button

    2024-03-24 07:26:01       16 阅读
  4. Wpf-定义状态

    2024-03-24 07:26:01       17 阅读
  5. WPF定义模版

    2024-03-24 07:26:01       12 阅读
  6. C#WPFButton详解

    2024-03-24 07:26:01       18 阅读
  7. wpf定义添加滚动条

    2024-03-24 07:26:01       34 阅读
  8. WPF定义,聚合器模式传递消息

    2024-03-24 07:26:01       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 07:26:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 07:26:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 07:26:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 07:26:01       18 阅读

热门阅读

  1. 【大厂面试题购物车】通关代码

    2024-03-24 07:26:01       17 阅读
  2. C++常用的库

    2024-03-24 07:26:01       17 阅读
  3. 基于FPGA的UDP协议栈设计第六章_仲裁模块设计

    2024-03-24 07:26:01       16 阅读
  4. leetcode - 362. Design Hit Counter

    2024-03-24 07:26:01       18 阅读
  5. v-if 遇到 el-form 表单验证规则遇到的bug

    2024-03-24 07:26:01       19 阅读
  6. c语言:日期识别1

    2024-03-24 07:26:01       17 阅读
  7. 单片机MCU,MPU,SOC的工艺结构原理及选型参数总结

    2024-03-24 07:26:01       21 阅读