需求:很多时候界面上的按钮都需要被贴上图片,一般来说:

1.按钮处于正常状态,按钮具有背景图A

2.鼠标移至按钮上方状态,按钮具有背景图B

3.鼠标点击按钮状态,按钮具有背景图C

4.按钮处于不可用状态,按钮具有背景图D

实现起来,毫无疑问,没什么难度。但是过程还是比较繁琐。这里我将这个过程封装为新的控件类:ImageButton

ImageButton中有四个属性(支持绑定),分别对应上面A、B、C、D四个背景图的路径。

#region 属性
/// <summary>
/// 按钮处于正常状态下的背景图片的路径
/// </summary>
public string NormalBackgroundImage
{
get { return ( string ) GetValue ( NormalBackgroundImageProperty ); } set { SetValue ( NormalBackgroundImageProperty, value ); }
} /// <summary>
/// 鼠标移到按钮上面,按钮的背景图片的路径
/// </summary>
public string MouseoverBackgroundImage
{
get { return ( string ) GetValue ( MouseoverBackgroundImageProperty ); } set { SetValue ( MouseoverBackgroundImageProperty, value ); }
} /// <summary>
/// 鼠标按下按钮,按钮的背景图片的路径
/// </summary>
public string MousedownBackgroundImage
{
get { return ( string ) GetValue ( MousedownBackgroundImageProperty ); } set { SetValue ( MousedownBackgroundImageProperty, value ); }
} /// <summary>
/// 当按钮不可用时按钮的背景图片
/// </summary>
public string DisabledBackgroundImage
{
get { return ( string ) GetValue ( DisabledBackgroundImageProperty ); } set { SetValue ( DisabledBackgroundImageProperty, value ); }
}
#endregion #region 依赖属性
/// <summary>
/// 按钮处于正常状态下的背景图片的路径(这是依赖属性)
/// </summary>
public static readonly DependencyProperty NormalBackgroundImageProperty =
DependencyProperty.Register ( "NormalBackgroundImage", typeof ( string ), typeof ( ImageButton ), new PropertyMetadata ( null ) ); /// <summary>
/// 鼠标移到按钮上面,按钮的背景图片的路径(这是依赖属性)
/// </summary>
public static readonly DependencyProperty MouseoverBackgroundImageProperty =
DependencyProperty.Register ( "MouseoverBackgroundImage", typeof ( string ), typeof ( ImageButton ), new PropertyMetadata ( null ) ); /// <summary>
/// 鼠标按下按钮,按钮的背景图片的路径(这是依赖属性)
/// </summary>
public static readonly DependencyProperty MousedownBackgroundImageProperty =
DependencyProperty.Register ( "MousedownBackgroundImage", typeof ( string ), typeof ( ImageButton ), new PropertyMetadata ( null ) ); /// <summary>
/// 当按钮不可用时按钮的背景图片(这是一个依赖属性)
/// </summary>
public static readonly DependencyProperty DisabledBackgroundImageProperty =
DependencyProperty.Register ( "DisabledBackgroundImage", typeof ( string ), typeof ( ImageButton ), new PropertyMetadata ( null ) );
#endregion

然后重写按钮的Style,Style保存在资源字典中:

<Style x:Key="ImageButtonStyle" TargetType="{x:Type local:ImageButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ImageButton}">
<Border x:Name="buttonBorder">
<Border.Background>
<ImageBrush ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=NormalBackgroundImage}"/>
</Border.Background>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="buttonBorder">
<Setter.Value>
<ImageBrush ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MouseoverBackgroundImage}"/>
</Setter.Value>
</Setter>
</Trigger> <Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="buttonBorder">
<Setter.Value>
<ImageBrush ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MousedownBackgroundImage}"/>
</Setter.Value>
</Setter>
</Trigger> <Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="buttonBorder">
<Setter.Value>
<ImageBrush ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DisabledBackgroundImage}"/>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

然后在构造函数中将按钮的Style改为写好的Style:

#region 构造函数
public ImageButton() : base()
{
//读取资源字典文件
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri ( "/Zmy.Wpf.Controls;component/Style.xaml", UriKind.Relative );
this.Resources.MergedDictionaries.Add ( rd );
//获取样式
this.Style = this.FindResource ( "ImageButtonStyle" ) as Style;
}
#endregion

通过这样的封装,图片按钮使用起来就非常的方便了:

            <StackPanel Orientation="Vertical">
<controls:ImageButton Height="80" Width="80"
NormalBackgroundImage="/Test;component/images/normal.png"
MousedownBackgroundImage="/Test;component/images/mousedown.png"
MouseoverBackgroundImage="/Test;component/images/mouseover.png"/> <controls:ImageButton Height="80" Width="80" IsEnabled="False"
DisabledBackgroundImage="/Test;component/images/disabled.png"/>
</StackPanel>

源代码下载地址:(不要积分)http://download.csdn.net/detail/lyclovezmy/7356841

WPF控件库:图片按钮的封装的更多相关文章

  1. 国内开源C# WPF控件库Panuon.UI.Silver推荐

    国内优秀的WPF开源控件库,Panuon.UI的优化版本.一个漂亮的.使用样式与附加属性的WPF UI控件库,值得向大家推荐使用与学习. 今天站长(Dotnet9,站长网址:https://dotne ...

  2. 国内开源C# WPF控件库Panuon.UI.Silver强力推荐

    国内优秀的WPF开源控件库,Panuon.UI的优化版本.一个漂亮的.使用样式与附加属性的WPF UI控件库,值得向大家推荐使用与学习. 今天站长(Dotnet9,站长网址:https://dotne ...

  3. 《Dotnet9》系列-开源C# WPF控件库3《HandyControl》强力推荐

    大家好,我是Dotnet9小编,一个从事dotnet开发8年+的程序员.我最近开始写dotnet分享文章,希望能让更多人看到dotnet的发展,了解更多dotnet技术,帮助dotnet程序员应用do ...

  4. 《Dotnet9》系列-开源C# WPF控件库2《Panuon.UI.Silver》强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  5. 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程

    反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑)   背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...

  6. 《Dotnet9》系列-开源C# WPF控件库1《MaterialDesignInXAML》强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  7. (四)开源C# WPF控件库《AduSkin – UI》

    微信公众号:[Dotnet9的博客],网站:[Dotnet9],问题或建议:[请网站留言], 如果对您有所帮助:[欢迎赞赏]. 开源C# WPF控件库系列: (一)开源C# WPF控件库<Mat ...

  8. WPF 控件库——仿制Chrome的ColorPicker

    WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...

  9. 我的WPF控件库——KAN.WPF.XCtrl(141105)

    自己开发的WPF控件库,只是初版,有扩展的Button,TextBox,Window.详细参见前几篇博文. WPF自定义控件(一)——Button:http://www.cnblogs.com/Qin ...

  10. WPF 控件库——仿制Windows10的进度条

    WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...

随机推荐

  1. Navicat 连接Oracle的教程以及注意事项

    今天使用Navicat 连接Oracle时晕倒了一些坑,特此记录一下! 楼主就是64位win10系统,安装的Navicat是64位的,刚开始配置32位的oci.配置后连接还是提示“Connot loa ...

  2. 全网最详细的基于Ubuntu14.04/16.04 + Anaconda2 / Anaconda3 + Python2.7/3.4/3.5/3.6安装Tensorflow详细步骤(图文)(博主推荐)

    不多说,直接上干货! 前言 建议参照最新的tensorflow安装步骤(Linux,官方网站经常访问不是很稳定,所以给了一个github的地址):         https://github.com ...

  3. linux 命令 — tr

    tr 对stdin字符进行替换.删除和压缩,基本形式 tr [options] set1 set2 将输入的字符串中的set1字符转换为set2中对应位置的字符 set1.set2表示字符集,如果se ...

  4. 翻译:非递归CTE(已提交到MariaDB官方手册)

    本文为mariadb官方手册:非递归CTE的译文. 原文:https://mariadb.com/kb/en/library/non-recursive-common-table-expression ...

  5. 举例分析 Makefile 中的 patsubst、wildcard、notdir 函数

    函数简介: 1.wildcard : 扩展通配符 2.notdir :去除路径 3.patsubst :替换通配符 实例: 建立一个 test 目录,在测试目录下建立一个名为 sub 的子目录 $ m ...

  6. Spring Cloud Finchley版中Consul多实例注册的问题处理

    由于Spring Cloud对Etcd的支持一直没能从孵化器中出来,所以目前来说大多用户还在使用Eureka和Consul,之前又因为Eureka 2.0不在开源的消息,外加一些博眼球的标题党媒体使得 ...

  7. .NET CORE 实践(3)--Visual Studio 2015 Update 3更新之后DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.exe无法正确安装

    打开 https://www.microsoft.com/net/core#windows,点击 https://go.microsoft.com/fwlink/?LinkId=691129下载vs2 ...

  8. 第一册:lesson fifteen。

    原文:Your passports,please. A:Are you Swedish? B:No,we are not. We are Danish. A:Are your friends Dani ...

  9. .NET 单元测试的利剑——模拟框架Moq(简述篇)

    .NET 单元测试的利剑--模拟框架Moq 前言 这篇文章是翻译文,因为通过自己参与的项目,越发觉得单元测试的重要性,特别是当跟业务数据打交道的时候的,Moq就如雪中送炭,所以想学习这个框架,就从这篇 ...

  10. PHP生成器细说

    之前写过关于生成器的文章,可能还不够详细,正好群里有朋友在讨论.觉得还是有必要再细说下,如果大家做过Python或者其他语言的,对于生成器应该不陌生.生成器是PHP 5.5.才引入的功能,也许大家觉得 ...