今天我们开始制作我们的按钮,主要的效果就是一个按钮正常状态、鼠标滑过、按下三态显示不同的图片。

首先我们需要给扩展按钮添加三个属性,分别是正常状态图片,鼠标滑过图片,按钮按下图片。

先贴出ButtonEx类

    public class ButtonEx : Button
{ static ButtonEx()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ButtonEx), new FrameworkPropertyMetadata(typeof(ButtonEx)));
} [CategoryAttribute("自定义属性"),DescriptionAttribute("获取或设置默认的背景图片")]
public ImageSource NormalImage
{
get { return (ImageSource)GetValue(NormalImageProperty); }
set { SetValue(NormalImageProperty, value); }
} // Using a DependencyProperty as the backing store for NormalImage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NormalImageProperty =
DependencyProperty.Register("NormalImage", typeof(ImageSource), typeof(ButtonEx), null); [CategoryAttribute("自定义属性"),DescriptionAttribute("获取或设置鼠标滑过时的背景图片")]
public ImageSource MouseOverImage
{
get { return (ImageSource)GetValue(MouseOverImageProperty); }
set { SetValue(MouseOverImageProperty, value); }
} // Using a DependencyProperty as the backing store for MouseOverImage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MouseOverImageProperty =
DependencyProperty.Register("MouseOverImage", typeof(ImageSource), typeof(ButtonEx), null); [CategoryAttribute("自定义属性"),DescriptionAttribute("获取或设置鼠按钮按下时的背景图片")]
public ImageSource PressImage
{
get { return (ImageSource)GetValue(PressImageProperty); }
set { SetValue(PressImageProperty, value); }
} // Using a DependencyProperty as the backing store for PressImage. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PressImageProperty =
DependencyProperty.Register("PressImage", typeof(ImageSource), typeof(ButtonEx), null); [Browsable(false)]
public new Brush Background
{
get { return base.Background; }
set { base.Background = value; }
} [Browsable(false)]
public new Brush BorderBrush
{
get { return base.BorderBrush; }
set { base.BorderBrush = value; }
}
}

我们这里定义了主要的三个属性

下面通过xaml来定义样式

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCustomControlLibrary1">
<Style TargetType="{x:Type local:ButtonEx}">
<!--Setter是一个设置器,用来设置该Style要对TargetType的哪些属性或对象进行设置,所以这里设置了ButtonEx属性NormalImage属性设置了一张图片-->
<Setter Property="NormalImage" Value="/WpfCustomControlLibrary1;component/Images/button_normal.png"></Setter>
<Setter Property="MouseOverImage" Value="/WpfCustomControlLibrary1;component/Images/button_hover.png"></Setter>
<Setter Property="PressImage" Value="/WpfCustomControlLibrary1;component/Images/button_pushed.png"></Setter>
<Setter Property="Width" Value="72"/>
<Setter Property="Height" Value="28"/>
<!--Template属性决定了将来ButtonEx的样式,该属性是ControlTemplate类型的-->
<Setter Property="Template">
<Setter.Value>
<!--实例化一个ControlTemplate实体,赋值给Template属性-->
<ControlTemplate TargetType="{x:Type local:ButtonEx}">
<Grid>
<!--用于指定控件处于特定状态时如何更改控件的外观,这是一组外观变化-->
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<!--设置状态变化所需要的时间-->
<VisualStateGroup.Transitions>
<!--需要声明从什么状态变为另一种状态及状态变化所需要的时间-->
<VisualTransition From="Noraml" To="MouseOver" GeneratedDuration="00:00:0.15" />
<VisualTransition From="MouseOver" To=" Normal" GeneratedDuration="00:00:0.15"/>
<VisualTransition From="Pressed" To="MouseOver" GeneratedDuration="00:00:0.15"/>
</VisualStateGroup.Transitions>
<!--定义状态,自然状态,一个状态下需要几个特性,需要使用动画板来控制-->
<VisualState x:Name="Normal">
<!--故事板能够用TargetProperty和TargetName属性指向一个特定的属性和特定的元素,
故事版在动画和希望应用动画的属性之间架起了一座桥梁,同一个故事版中放置几个动画,
并且每个动画可以用于不同的元素和属性-->
<!--一个控制板将属性和动画联系在一起,当为自然状态下,将自然状态下的图片Opacity属性设置为1,然后将鼠标滑过和按下状态Opacity设置成0,
注意:一定要将后两种状态Opacity设置成0,不然有可能是1,这样可能得到我们不想要的效果-->
<Storyboard>
<!--动画有两种,一种是在一个开始值和结束值之间以逐步增加的方式(被称为线性插值过程)改变属性的动画,
以及从一个值突然变成另一个值的动画,第一种很好理解,第二种都是使用关键帧动画的技术,多数以
"类型名+AnimationUsingKeyFrames"的形式命名的,下面使用的就是这种动画类型-->
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_Normal" Storyboard.TargetProperty="(Opacity)">
<!--获取或设置应到达关键帧的目标 System.Windows.Media.Animation.DoubleKeyFrame.Value 的时间。获取或设置关键帧的目标值。-->
<EasingDoubleKeyFrame KeyTime="0" Value="1"></EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_MouseOver" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0" Value="0"></EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_Pressed" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0" Value="0"></EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<!--鼠标按下状态-->
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_Pressed" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0" Value="1">
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_Normal" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0" Value="0">
</EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_MouseOver" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0" Value="0"></EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_MouseOver" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0" Value="1"></EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_Normal" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="PART_Pressed" Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<!--这里放了一张图片元素,所以正常情况下,NormalImage属性图片会显示到按钮上-->
<Image Source="{TemplateBinding NormalImage}" Name="PART_Normal"></Image>
<Image Source="{TemplateBinding PressImage}" Name="PART_Pressed"></Image>
<Image Source="{TemplateBinding MouseOverImage}" Name="PART_MouseOver"></Image>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
<!--绑定了ButtonEx的Content属性-->
<ContentPresenter Content="{TemplateBinding Content}"></ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

所有的难以理解的都加上注释了

WPF自定义控件(三)的更多相关文章

  1. WPF自定义控件三:消息提示框

    需求:实现全局消息提示框 一:创建全局Message public class Message { private static readonly Style infoStyle = (Style)A ...

  2. WPF自定义控件(三)の扩展控件

    扩展控件,顾名思义就是对已有的控件进行扩展,一般继承于已有的原生控件,不排除继承于自定义的控件,不过这样做意义不大,因为既然都自定义了,为什么不一步到位呢,有些不同的需求也可以通过此来完成,不过类似于 ...

  3. WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    一.前言.预览 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要是对文本 ...

  4. WPF自定义控件与样式(1)-矢量字体图标(iconfont)

    一.图标字体 图标字体在网页开发上运用非常广泛,具体可以网络搜索了解,网页上的运用有很多例子,如Bootstrap.但在C/S程序中使用还不多,字体图标其实就是把矢量图形打包到字体文件里,就像使用一般 ...

  5. WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: Che ...

  6. WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 日历控 ...

  7. WPF自定义控件与样式(6)-ScrollViewer与ListBox自定义样式

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: Scr ...

  8. WPF自定义控件与样式(7)-列表控件DataGrid与ListView自定义样式

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: Dat ...

  9. WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...

随机推荐

  1. jmeter uniq 取值方式设置

  2. sed查找实例:mysql_process.sh

    标准 #!/bin/bash # FILE_NAME=/home/roo/Desktop/shell_code/day6/my.cnf # 获取所有的片段 function get_all_segme ...

  3. vs code配置C/C++开发环境

    第一步:下载 Vs Code 点击链接下载Vs Code 下载版本  并安装 https://code.visualstudio.com/ 点击 Download for Windwos 安装时  如 ...

  4. mybatis报错:org.apache.ibatis.builder.BuilderException:Encountered " "shr" "shr " " at line 1,column 1

    程序报错如下: 解决:变量名冲突 表字段‘审核人’简称为shr,与mybatis的OGNL表达式发生冲突. 解决方法:修改冲突的变量名即可. 总结了一下变量命名可能发生冲突的变量集合: bor(字符| ...

  5. 12、numpy——数学函数

    NumPy 数学函数 NumPy 包含大量的各种数学运算的函数,包括三角函数,算术运算的函数,复数处理函数等. 1.三角函数 NumPy 提供了标准的三角函数:sin().cos().tan(). i ...

  6. Ride to Office(贪心水题)

    [题目链接] http://noi.openjudge.cn/ch0406/2404/ [算法] 一开始zz了,先按时间排序然后如果速度超过当前男主速度,且在男主到达目的地前超过男主则最终男主和这个人 ...

  7. Kvm --05 密码保护:Kvm管理之WebVirtMgr

    目录 密码保护:Kvm管理之WebVirtMgr 1. 前言 2. 特点 3. 功能 4. 部署 1).安装相关依赖 2).安装Python需求环境 3).配置Nginx 4). 远程连接 5).更新 ...

  8. ELKStack之生产案例(下)

    ELKStack之生产案例(下) 链接:https://pan.baidu.com/s/1V2aYpB86ZzxL21Hf-AF1rA 提取码:7izv 复制这段内容后打开百度网盘手机App,操作更方 ...

  9. ltp-ddt realtime_cpu_load涉及的cyclictest 交叉编译

    Cyclictest 是 rt-tests 下的一个测试工具,也是rt-tests 下使用最广泛的测试工具,一般主要用来测试使用内核的延迟,从而判断内核的实时性.   1.下载源码 git clone ...

  10. 什么是NFA(不确定的有穷自动机)和DFA(确定的有穷自动机)

    本节知识点是<编译原理>第三章-词法分析,学习参考教材为清华大学出版社<编译原理>第三版: 前情提要: 字母表∑1和∑2的乘积( product): ∑1∑2 ={ab|a ∈ ...