当前位置 博文首页 > 文章内容

    C# WPF 自定义按钮的方法

    作者:shunshunshun18 栏目:未分类 时间:2021-03-03 14:43:16

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    本文介绍WPF一种自定义按钮的方法。

    实现效果

    1. 使用图片做按钮背景;
    2. 自定义鼠标进入时效果;
    3. 自定义按压效果;
    4. 自定义禁用效果

    实现效果如下图所示:

    实现步骤

    1. 创建CustomButton.cs,继承自Button;
    2. 创建一个资源文件ButtonStyles.xaml;
    3. 在资源文件中设计按钮的Style;
    4. 在CustomButton.cs中添加Style中需要的依赖属性;
    5. 在程序中添加资源并引用(为了方便在不同的程序中引用自定义按钮,自定义按钮放在独立的类库中,应用程序中进行资源合并即可)。

    示例代码

    // ButtonStyles.xaml
    <Style x:Key="CustomButton" TargetType="{x:Type local:CustomButton}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type local:CustomButton}">
            <Grid x:Name="container">
              <Image Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" 
                  Source="{Binding ButtonImage,RelativeSource={RelativeSource Mode=TemplatedParent}}">
                <Image.RenderTransformOrigin>
                  <Point X="0.5" Y="0.5"/>
                </Image.RenderTransformOrigin>
                <Image.RenderTransform>
                  <ScaleTransform x:Name="scaletrans" ScaleX="1" ScaleY="1"/>
                </Image.RenderTransform>
              </Image>
            </Grid>
            <ControlTemplate.Triggers>
              <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="0.5" TargetName="container"/>
              </Trigger>
              <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#2c000000" TargetName="container"/>
              </Trigger>
              <Trigger Property="IsPressed" Value="True">
                <Trigger.EnterActions>
                  <BeginStoryboard>
                    <Storyboard>
                      <DoubleAnimation Storyboard.TargetName="scaletrans" Storyboard.TargetProperty="(ScaleTransform.ScaleX)" 
                      To="0.8" Duration="0:0:0.15" AutoReverse="True"/>
                      <DoubleAnimation Storyboard.TargetName="scaletrans" Storyboard.TargetProperty="(ScaleTransform.ScaleY)" 
                      To="0.8" Duration="0:0:0.15" AutoReverse="True"/>
                    </Storyboard>
                  </BeginStoryboard>
                </Trigger.EnterActions>
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    
    // CustomButton.cs
    public class CustomButton : Button
    {
      public ImageSource ButtonImage
      {
        get { return (ImageSource)GetValue(ButtonImageProperty); }
        set { SetValue(ButtonImageProperty, value); }
      }
    
      public static readonly DependencyProperty ButtonImageProperty =
        DependencyProperty.Register("ButtonImage", typeof(ImageSource), typeof(CustomButton),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
    }
    
    // App.xaml 合并资源
    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source=".../ButtonStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>   
    </Application.Resources>
    
    // view.xaml 使用
    <Grid>
      <customcontrols:CustomButton Width="48" Height="48" 
        Style="{StaticResource CustomButton}" ButtonImage="/Louzi.Paint;component/Images/Toolbar/write.png"/>
    </Grid>

    以上就是C# WPF 自定义按钮的方法的详细内容,更多关于C# WPF 自定义按钮的资料请关注IIS7站长之家博文其它相关文章!