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

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. 工作随笔—Elasticsearch大量数据提交优化

    问题:当有大量数据提交到Elasticsearch时,怎么优化处理效率? 回答: 批量提交 当有大量数据提交的时候,建议采用批量提交. 比如在做 ELK 过程中 ,Logstash indexer 提 ...

  2. .Net 从零开始构建一个框架之基本实体结构与基本仓储构建

    本系列文章将介绍如何在.Net框架下,从零开始搭建一个完成CRUD的Framework,该Framework将具备以下功能,基本实体结构(基于DDD).基本仓储结构.模块加载系统.工作单元.事件总线( ...

  3. DDD实战进阶第一波(十三):开发一般业务的大健康行业直销系统(订单上下文领域逻辑)

    前一篇文章主要讲了订单上下文的POCO模型,其中订单与订单项中有大量的值对象.这篇文章主要讲讲这些值对象以及订单项.订单相关的领域逻辑. 1.ProductSKUs值对象领域逻辑:ProductSKU ...

  4. pip install 安装出现问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position XX的解决办法

    pip install 安装出现问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position XX的解决办法 转自c ...

  5. Nginx编译安装lua-nginx-module

    lua-nginx-module 模块可以将Lua的强大功能嵌入NGINX服务器. 下载Nginx源码 如果已安装Nginx,需要查看当前安装版本的编译参数: $ /usr/local/nginx/s ...

  6. Gradle安装使用以及基本操作

    这两天看到越来越多的在接触Gradle,然后发现我之前没有做过记录,而且之后下个月的一些有关SpringBoot的东西也需要用到,所以这里就来记录一下,方便以后使用. 简单介绍 Gradle是一个好用 ...

  7. .NET西安社区 [拥抱开源,又见 .NET] 活动简报

    拥抱开源, 又见 .NET」 随着 .NET Core的发布和开源,.NET又重新回到了人们的视野.除了开源.跨平台.高性能以及优秀的语言特性,越来越多的第三方开源库也出现在了Github上——包括M ...

  8. 【原创】STM32工程新建步骤

    1. 新建文件夹          DOC文件夹:        可以存放readme.txt等项目文档     Library文件夹:    可以存放ST库源码,直接复制ST库的源代码到Librar ...

  9. 我们来说一说TCP神奇的40ms

    本文由云+社区发表 TCP是一个复杂的协议,每个机制在带来优势的同时也会引入其他的问题. Nagel算法和delay ack机制是减少发送端和接收端包量的两个机制, 可以有效减少网络包量,避免拥塞.但 ...

  10. 【Flask-RESTPlus系列】Part3:请求解析

    0x00 内容概览 请求解析 基本参数 必需参数 多值和列表 其他目标 参数位置 参数多个位置 高级类型处理 解析器继承 文件上传 错误处理 错误消息 参考链接 0x01 请求解析 注意:Flask- ...