WPF之创建自定义按钮CustButton

创建自定义控件文件夹-CustControl

在这里插入图片描述

创建用户控件-CustButton

在这里插入图片描述

再改Button

在这里插入图片描述

在这里插入图片描述

<Button.Resources>
    <ResourceDictionary Source="pack://application:,,,/ModuleView;component/SummaryStyle.xaml"/>
</Button.Resources>

UI

   <Button.Template>
       <ControlTemplate TargetType="Button">
           <Grid ClipToBounds="True" MouseLeftButtonDown="Grid_MouseLeftButtonDown">
               <Border Name="back" Style="{StaticResource borderStyle}"  CornerRadius="6">
                   <ContentPresenter 
                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                           Content="{TemplateBinding Content}"/>
               </Border>
               <Path Fill="White" x:Name="MyPath">
                   <Path.Data>
                       <EllipseGeometry x:Name="MyEllipse" RadiusX="0" RadiusY="{Binding RelativeSource={RelativeSource Mode=Self},Path=RadiusX}"/>
                   </Path.Data>
               </Path>
           </Grid>
           <ControlTemplate.Triggers>
               <Trigger Property="IsMouseOver" Value="True">
                   <Setter TargetName="back" Property="Visibility" Value="Visible"/>
                   <Setter TargetName="back" Property="Background" Value="Gray"/>
               </Trigger>
               <Trigger Property="IsEnabled" Value="false">
                   <Setter TargetName="back" Property="Background" Value="#44FFFFFF"/>
               </Trigger>
           </ControlTemplate.Triggers>
       </ControlTemplate>
   </Button.Template>
   <Border>

   </Border>

事件

     private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
     {
         var target = Template.FindName("MyEllipse", this) as EllipseGeometry;
         target.Center = Mouse.GetPosition(this);
         var animation = new DoubleAnimation()
         {
             From = 0,
             To = 150,
             Duration = new Duration(TimeSpan.FromSeconds(1)),
         };

         target.BeginAnimation(EllipseGeometry.RadiusXProperty, animation);

         var animation2 = new DoubleAnimation()
         {
             From = 0.3,
             To = 0,
             Duration = new Duration(TimeSpan.FromSeconds(1)),
         };

         var target2 = Template.FindName("MyPath", this) as Path;

         target2.BeginAnimation(Path.OpacityProperty, animation2);

     }

使用

       <local:CustButton 
        Background="Transparent"
        Grid.Column="1"  
        Width="80" Height="32"  
        FontSize="13" 
        Foreground="White" 
        Content="取消" 
        Command="{Binding CancelFrm}"
        CommandParameter="{Binding ElementName=CreatePrj}"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值