温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在C#项目中使用WPF自定义按钮

发布时间:2021-03-03 15:35:42 来源:亿速云 阅读:208 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关如何在C#项目中使用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自定义按钮有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

wpf
AI