WPF图片按钮的实现方法

所属分类: 网络编程 / ASP.NET 阅读数: 499
收藏 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"/>

效果展示

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

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

ASP.NET Web API教程 创建Admin视图详细介绍

现在我们转入客户端,并添加一个能够使用从Admin控制器而来的数据的页面。通过给控制器发送AJAX请求的方式,该页面将允许用户创建、编辑,或删除产品
收藏 0 赞 0 分享

asp.net中session的原理及应用详解

Session是一种Web会话中的常用状态之一,Session提供了一种把信息保存在服务器内存中的方式。他能储存任何数据类型,包含自定义对象,本文将详细介绍asp.net中session的原理及应用,需要的朋友可以参考下
收藏 0 赞 0 分享

Web开发异常行为排查常用方法图文介绍

平常程序遇到错误,开发环境下一般都用调试搞定,生产环境下通过查看日志搞定。但也有搞不定的时候,本文提供了详细的解决方案
收藏 0 赞 0 分享

ASP.NET repeater添加序号列的方法

在项目开发过程中,会经常遇到ASP.NET repeater控件添加序号列,有些新手可能还不会,网上搜集整理了一些,需要的朋友可以参考下
收藏 0 赞 0 分享

GridView常用操作事件图文介绍

对于gridview学NET的同学再熟悉不过,但是其中功能事件是否能编码熟练实现
收藏 0 赞 0 分享

批量账号的login测试功能实现

用WaitiN写了个简单的login自动化测试,能够使用少量的代码实现批量账号的login测试,需要的朋友可以参考下
收藏 0 赞 0 分享

iis配置asp.net常见问题解决方案

如何解决iis配置asp.net常见一些问题,根据自己的使用经验,总结了一些,希望可以帮助你问
收藏 0 赞 0 分享

.net自带的库生成zip文件的方法

平时我们创建Zip文件的时候,要么用现成的软件,要么用第三方的开源库。其实用.net自带的类操作起来也非常方便
收藏 0 赞 0 分享

asp.net中Post表单保存页面状态并输出源码的实现方法

先执行脚本,复制源码到隐藏域里,再输出源码,注意代码红色设置
收藏 0 赞 0 分享

用Html5与Asp.net MVC上传多个文件的实现代码

Html 5 的有一些File API,对Form表单增强的特性,让我们轻松支持多文件上传,看下面的Html片断代码
收藏 0 赞 0 分享
查看更多