WPF图片按钮的实现方法

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

直接代码

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

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

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

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

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

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

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

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

 }

样式代码


调用实例

复制代码 代码如下:
 

效果展示

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

你可能感兴趣的:(WPF图片按钮的实现方法)