WPF图片按钮的实现方法

所属分类: 网络编程 / ASP.NET 阅读数: 504
收藏 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实现XML与DataTable互转的实例代码

.NET实现XML与DataTable互转的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

使用DataAdapter填充多个表(利用DataRelation)的实例代码

使用DataAdapter填充多个表(利用DataRelation)的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

Repeater全选删除和分页实现思路及代码

Repeater控件想必熟悉.net web开发的人员是很了解不过的了,接下来将与大家共同学习下它的全选删除和分页,感兴趣的你可不要错过了哈,希望可以帮助到你
收藏 0 赞 0 分享

.net搜索查询并实现分页实例

.net搜索查询并实现分页实例,需要的朋友可以参考一下
收藏 0 赞 0 分享

Repeater对数据进行格式化处理

最近不止一个同学,问我在Repeater里怎么格式化数据,怎么处理。因为Repeater 属于服务器端控件。要么利用本身的控件事件来处理,要么在数据源上处理。
收藏 0 赞 0 分享

合并两个DataSet的数据内容的方法

合并两个DataSet的数据内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

.NET运行界面上,实现随意拖动控件的方法

.NET运行界面上,实现随意拖动控件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

ASP.NET实现License Key输入功能的小例子

当我们安装微软的软件,多数软件是需要输入license key。它有五个文本框,输入完第一个文本框之后,光标自动跳至下一个文本框。 Insus.NET今天也使用asp.net来模仿一个。呵呵。
收藏 0 赞 0 分享

asp.net中将js的返回值赋给asp.net控件的小例子

要做一个显示用户在线停留时间的功能,拖了一个label控件用于显示时间,而时间是通过js来实现的,现在要把js的返回值赋给label,方法如下:
收藏 0 赞 0 分享

GridView高效分页和搜索功能的实现代码

GridView高效分页和搜索功能的实现代码,需要的朋友可以参考一下
收藏 0 赞 0 分享
查看更多