WPF图片按钮的实现方法

所属分类: 网络编程 / ASP.NET 阅读数: 512
收藏 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 JavaScript插件  JavaScript Function Outliner

一个JavaScript Function Outliner插件 第四版本 支持内嵌javascript,且可以对javascript进行压缩
收藏 0 赞 0 分享

asp.net汉字转拼音和获取汉字首字母的代码

在网上找到的好东西。以后asp.net下汉字转成拼音就方便多了
收藏 0 赞 0 分享

asp.net 多字段模糊查询代码

经常用到多字段的模糊查询,上面的函数可以实现,例如strKeyWords值为“脚本之家”时
收藏 0 赞 0 分享

OpenCms 带分页的新闻列表

有一些网友在新闻列表分页上还遇到一些问题,正好这个blog上也忘记了此部分内容,现在补充上,功能是实现了,可以自己再做些优化,OpenCms7.0.5下测试通过,内容如下(编辑器的插入代码功能有问题,就直接把代码粘上了
收藏 0 赞 0 分享

URLRewriter最简单入门介绍 URLRewriter相关资源

配置好后,查看日志看到的状态都是200,IIS直接认为这个文件是存在的了, 而不是301,或302,这在某些情况下可能会不适用,比如:搜索引擎优化时目录或文件调整。
收藏 0 赞 0 分享

asp.net Repeater取得CheckBox选中的某行某个值

Repeater取得CheckBox选中的某行某个值的实现代码
收藏 0 赞 0 分享

asp.net清空Cookie的两种方法

清空cookie
收藏 0 赞 0 分享

asp.net一些很酷很实用的.Net技巧第1/2页

方便使用asp.net编程的朋友,都是一些非常有用的东西
收藏 0 赞 0 分享

asp.net下URL网址重写成.html格式、RSS、OPML的知识总结

asp.net下URL网址重写成.html格式、RSS、OPML的知识总结
收藏 0 赞 0 分享

使用UserControl做网站导航条的思路 分析

使用UserControl做网站导航条的思路 分析
收藏 0 赞 0 分享
查看更多