WPF图片按钮的实现方法

所属分类: 网络编程 / ASP.NET 阅读数: 474
收藏 0 赞 0 分享

本文实例为大家分享了WPF图片按钮的实现代码,供大家参考,具体内容如下

直接代码

public class ImageButton : System.Windows.Controls.Button
 {

 /// <summary>
 /// 图片
 /// </summary>
 public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton),
  new PropertyMetadata(null));

 /// <summary>
 /// 图片的宽度
 /// </summary>
 public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register("ImageWidth", typeof(double), typeof(ImageButton),
  new PropertyMetadata(double.NaN));

 /// <summary>
 /// 图片的高度
 /// </summary>
 public static readonly DependencyProperty ImageHeightProperty = DependencyProperty.Register("ImageHeight", typeof(double), typeof(ImageButton),
  new PropertyMetadata(double.NaN));

 /// <summary>
 /// 构造函数
 /// </summary>
 static ImageButton()
 {
  DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), 
  new System.Windows.FrameworkPropertyMetadata(typeof(ImageButton)));
 }

 /// <summary>
 /// 设置图片
 /// </summary>
 public ImageSource Image
 {
  get
  {
  return GetValue(ImageProperty) as ImageSource;
  }
  set
  {
  SetValue(ImageProperty, value);
  }
 }

 /// <summary>
 /// 图片宽度(属性)
 /// </summary>
 public double ImageWidth
 {
  get
  {
  return (double)GetValue(ImageWidthProperty);
  }
  set
  {
  SetValue(ImageWidthProperty, value);
  }
 }

 /// <summary>
 /// 图片高度(属性)
 /// </summary>
 public double ImageHeight
 {
  get
  {
  return (double)GetValue(ImageHeightProperty);
  }
  set
  {
  SetValue(ImageHeightProperty, value);
  }
 }

 }

样式代码

<Style TargetType="{x:Type xi:ImageButton}">
 <Setter Property="Template">
  <Setter.Value>
  <ControlTemplate TargetType="{x:Type xi:ImageButton}">
   <Grid>
   <Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="Auto"/>
   </Grid.RowDefinitions>
   <Border x:Name="border" Grid.RowSpan="2" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 
    SnapsToDevicePixels="true" CornerRadius="3,3,3,3"/>
   <Image Grid.Row="0" Source="{TemplateBinding Image}"
     Width="{TemplateBinding ImageWidth}"
     Height="{TemplateBinding ImageHeight}"
     VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
   <ContentPresenter Grid.Row="1" HorizontalAlignment="Center" Margin="{TemplateBinding Padding}" 
     VerticalAlignment="Center" RecognizesAccessKey="True" />
   </Grid>
   <ControlTemplate.Triggers>
   <Trigger Property="IsPressed" Value="True">
    <Setter Property="Foreground" Value="#999999"/>
   </Trigger>
   </ControlTemplate.Triggers>

  </ControlTemplate>
  </Setter.Value>
 </Setter>
 </Style>

调用实例

复制代码 代码如下:
 <xi:ImageButton Image="../Image/设置.png" Content="新增会员" ImageHeight="52" ImageWidth="52" Width="72" Height="72" Margin="30,10,10,10"/>

效果展示

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

详解.NET中的加密算法总结(自定义加密Helper类续)

这篇文章主要介绍了详解.NET中的加密算法总结(自定义加密Helper类续) ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

详谈.NET的异常处理

本文在对异常的介绍中,主要介绍了CLR的异常处理机制,一些较为通用的异常代码,以及对Exception类的介绍。具有很好的参考价值,需要的朋友一起来看下吧
收藏 0 赞 0 分享

浅谈Main方法的参数

本文主要对Main方法的参数通过案例分析进行介绍,具有很好的参考价值,需要的朋友一起来看下吧
收藏 0 赞 0 分享

详解.net循环、逻辑语句块(基础知识)

本篇是介绍.NET 基础部分,主要简述循环,判断,对初学者具有很好的参考借鉴价值,下面就跟小编一起来看下吧
收藏 0 赞 0 分享

BaiduTemplate模板引擎使用示例(附源码)

本文主要分享了BaiduTemplate模板引擎使用示例,具有很好的参考价值,需要的朋友一起来看下吧
收藏 0 赞 0 分享

Asp.net中通过Button打开另一个的frm

本文通过实例代码给大家介绍了asp.net中通过button打开另一个frm的方法,非常不错,需要的朋友参考下吧
收藏 0 赞 0 分享

.NET framework 4.0 安装失败回滚问题

这篇文章主要介绍了.NET framework 4.0 安装失败回滚问题,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

.NET实现文件跨服务器上传下载的方法

这篇文章主要给大家介绍了.NET文件如何实现跨服务器上传下载的方法,文中通过图片介绍的很详细,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友们可以跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

ASP.NET操作MySql数据库的实例代码讲解

这篇文章主要介绍了ASP.NET操作MySql数据库的实例代码讲解,需要的朋友可以参考下
收藏 0 赞 0 分享

Asp.net Core 1.1 升级后操作mysql出错的解决办法

这篇文章主要介绍了Asp.net Core 1.1 升级后操作mysql出错的解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多