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

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. 程序员周末阿里面试,5分钟就被一道题秒杀:HashMap与Hashtable

    你们可能会想,我这么菜的吗?5分钟都坚持不了? 本文说起来会有点尴尬,毕竟这是我曾经经历过的故事 那时候的我还真菜,每天写着 if/ for 及一些简单的业务逻辑代码,虽工作有些日子了,但技术水平还停 ...

  2. kindeditor扩展粘贴截图功能&修改图片上传路径并通过webapi上传图片到图片服务器

    前言 kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 而kindeditor却对图片的处理不够理想. 本篇博文需要解决的问题有两个: kindeditor扩展粘贴图片功 ...

  3. VP9 vs H.265——下一代视频编码标准的王道之争

    目前下一代主流的视频编码标准有 ITU-T VCEG 推出来的 H.265 和 Google 推出 VP9 . H.265 在 H.264 的基础上保留其中的部分技术,并对相关技术加以改进研发而成.新 ...

  4. vsCode 设置vue 保存自动格式化代码

    setting { // vscode默认启用了根据文件类型自动设置tabsize的选项 "editor.detectIndentation": false, // 重新设定tab ...

  5. MQ(2)---JMS

    JMS 一.理解JMS   1.什么是JMS?         JMS即Java消息服务(Java Message Service)应用程序接口,API是一个消息服务的标准或者说是规范,允许应用程序组 ...

  6. linux overcommit flag

    linux中有一个overcomit的配置,这个配置关系到进程在过多申请memory资源的时候,系统的表现(启发式允许,不检查,or 阻止) /proc/sys/vm/overcommit_memor ...

  7. TypeError: Cannot red property 'style' of null 错误解决

    错误信息如下: JSP代码如下: <c:if test ="${not empty excelErrors}"> <div id="excelError ...

  8. Java多线程之一

    进程与线程 进程 进程是进程实体的运行过程,是系统进行资源分配和调度的一个独立单位,比如我们windows电脑上运行的一个程序就是一个进程.在传统进程中进程是资源分配和调度的一个基本单位,在后来引入线 ...

  9. SVN用户切换

     Eclipse的SVN插件Subclipse做得很好,在svn操作方面提供了很强大丰富的功能.但到目前为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,而且一旦用户的帐号.密码保存之后 ...

  10. jquery的jsonp的使用

    客户端 $(function(){ $.ajax({ type: "get", async: false, url: "http://test.com/json_data ...