第一种:

因为项目需要制作ToggleButton图片按钮,要求点击之后改变图片,而Check和Uncheck的时候又具有Normal、MouseOver、Pressed三种状态。

开始想要利用VisualStateManager来做,如果放置6个image有不知道怎么区分check状态和UnCheck状态的Normal、MouseOver、Pressed(如果有知道的告诉一声,谢谢)。

然后想了一个比较笨的办法,制作一个图片按钮,区分三种状态,然后在ToggleButton图片按钮里面放置两个图片按钮,但是这样的话,图片按钮会阻止点击是的状态变化,于是对每个按钮实现click事件,手动实现状态变化。代码如下:

xaml:

<ToggleButton x:Class="DrawText.Controls.ImageToggleButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:imageBtn="clr-namespace:Controls"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <ToggleButton.Style>
        <Style TargetType="ToggleButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CheckStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Checked">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                           Storyboard.TargetName="check">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                           Storyboard.TargetName="unCheck">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                           
                            <imageBtn:ImageButton Name="unCheck"
                                NormalImage="{Binding UnCheckNormalImage,RelativeSource={RelativeSource TemplatedParent}}"
                                HoverImage="{Binding UnCheckMouseOverImage,RelativeSource={RelativeSource TemplatedParent}}"
                                PressedImage="{Binding UncheckPressedImage,RelativeSource={RelativeSource TemplatedParent}}"
                                                  Click="unCheck_Click"/>
                            <imageBtn:ImageButton Name="check" Visibility="Collapsed"
                                NormalImage="{Binding CheckNormalImage,RelativeSource={RelativeSource TemplatedParent}}"
                                HoverImage="{Binding CheckMouseOverImage,RelativeSource={RelativeSource TemplatedParent}}"
                                PressedImage="{Binding CheckPressedImage,RelativeSource={RelativeSource TemplatedParent}}"
                                                  Click="check_Click"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ToggleButton.Style>
</ToggleButton>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DrawText.Controls
{
    /// <summary>
    /// ImageToggleButton.xaml 的交互逻辑
    /// </summary>
    public partial class ImageToggleButton : ToggleButton
    {
        public ImageToggleButton()
        {
            InitializeComponent();
        }

public ImageSource CheckNormalImage
        {
            get { return (ImageSource)GetValue(CheckNormalImageProperty); }
            set { SetValue(CheckNormalImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for CheckNormalImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CheckNormalImageProperty =
            DependencyProperty.Register("CheckNormalImage", typeof(ImageSource), typeof(ImageToggleButton), new PropertyMetadata(null));

public ImageSource CheckMouseOverImage
        {
            get { return (ImageSource)GetValue(CheckMouseOverImageProperty); }
            set { SetValue(CheckMouseOverImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for CheckMouseOverImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CheckMouseOverImageProperty =
            DependencyProperty.Register("CheckMouseOverImage", typeof(ImageSource), typeof(ImageToggleButton), new PropertyMetadata(null));

public ImageSource CheckPressedImage
        {
            get { return (ImageSource)GetValue(CheckPressedImageProperty); }
            set { SetValue(CheckPressedImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for CheckPressedImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CheckPressedImageProperty =
            DependencyProperty.Register("CheckPressedImage", typeof(ImageSource), typeof(ImageToggleButton), new PropertyMetadata(null));

public ImageSource UnCheckNormalImage
        {
            get { return (ImageSource)GetValue(UnCheckNormalImageProperty); }
            set { SetValue(UnCheckNormalImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for UnCheckNormalImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UnCheckNormalImageProperty =
            DependencyProperty.Register("UnCheckNormalImage", typeof(ImageSource), typeof(ImageToggleButton), new PropertyMetadata(null));

public ImageSource UnCheckMouseOverImage
        {
            get { return (ImageSource)GetValue(UnCheckMouseOverImageProperty); }
            set { SetValue(UnCheckMouseOverImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for UnCheckMouseOverImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UnCheckMouseOverImageProperty =
            DependencyProperty.Register("UnCheckMouseOverImage", typeof(ImageSource), typeof(ImageToggleButton), new PropertyMetadata(null));

public ImageSource UncheckPressedImage
        {
            get { return (ImageSource)GetValue(UncheckPressedImageProperty); }
            set { SetValue(UncheckPressedImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for UncheckPressedImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UncheckPressedImageProperty =
            DependencyProperty.Register("UncheckPressedImage", typeof(ImageSource), typeof(ImageToggleButton), new PropertyMetadata(null));

private void unCheck_Click(object sender, RoutedEventArgs e)
        {
            this.IsChecked = true;
        }

private void check_Click(object sender, RoutedEventArgs e)
        {
            this.IsChecked = false;
        }

}
}

第二种:

该方法使触发器实现

xaml:

<ToggleButton x:Class="DrawText.Controls.TriggerImageButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <ToggleButton.Style>
        <Style TargetType="ToggleButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Image Name="image"/>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter Property="Source" TargetName="image"
                                        Value="{Binding CheckNormalImage,RelativeSource={RelativeSource TemplatedParent}}"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="false">
                                <Setter Property="Source" TargetName="image"
                                        Value="{Binding UnCheckNormalImage,RelativeSource={RelativeSource TemplatedParent}}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsChecked" Value="true"/>
                                    <Condition Property="IsMouseOver" Value="true"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Source" TargetName="image"
                                        Value="{Binding CheckMouseOverImage,RelativeSource={RelativeSource TemplatedParent}}"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsChecked" Value="true"/>
                                    <Condition Property="IsPressed" Value="true"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Source" TargetName="image"
                                        Value="{Binding CheckPressedImage,RelativeSource={RelativeSource TemplatedParent}}"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsChecked" Value="false"/>
                                    <Condition Property="IsMouseOver" Value="true"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Source" TargetName="image"
                                        Value="{Binding UnCheckMouseOverImage,RelativeSource={RelativeSource TemplatedParent}}"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsChecked" Value="false"/>
                                    <Condition Property="IsPressed" Value="true"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Source" TargetName="image"
                                        Value="{Binding UncheckPressedImage,RelativeSource={RelativeSource TemplatedParent}}"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ToggleButton.Style>
</ToggleButton>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DrawText.Controls
{
    /// <summary>
    /// TriggerImageButton.xaml 的交互逻辑
    /// </summary>
    public partial class TriggerImageButton : ToggleButton
    {
        public TriggerImageButton()
        {
            InitializeComponent();
        }

public ImageSource CheckNormalImage
        {
            get { return (ImageSource)GetValue(CheckNormalImageProperty); }
            set { SetValue(CheckNormalImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for CheckNormalImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CheckNormalImageProperty =
            DependencyProperty.Register("CheckNormalImage", typeof(ImageSource), typeof(TriggerImageButton), new PropertyMetadata(null));

public ImageSource CheckMouseOverImage
        {
            get { return (ImageSource)GetValue(CheckMouseOverImageProperty); }
            set { SetValue(CheckMouseOverImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for CheckMouseOverImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CheckMouseOverImageProperty =
            DependencyProperty.Register("CheckMouseOverImage", typeof(ImageSource), typeof(TriggerImageButton), new PropertyMetadata(null));

public ImageSource CheckPressedImage
        {
            get { return (ImageSource)GetValue(CheckPressedImageProperty); }
            set { SetValue(CheckPressedImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for CheckPressedImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CheckPressedImageProperty =
            DependencyProperty.Register("CheckPressedImage", typeof(ImageSource), typeof(TriggerImageButton), new PropertyMetadata(null));

public ImageSource UnCheckNormalImage
        {
            get { return (ImageSource)GetValue(UnCheckNormalImageProperty); }
            set { SetValue(UnCheckNormalImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for UnCheckNormalImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UnCheckNormalImageProperty =
            DependencyProperty.Register("UnCheckNormalImage", typeof(ImageSource), typeof(TriggerImageButton), new PropertyMetadata(null));

public ImageSource UnCheckMouseOverImage
        {
            get { return (ImageSource)GetValue(UnCheckMouseOverImageProperty); }
            set { SetValue(UnCheckMouseOverImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for UnCheckMouseOverImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UnCheckMouseOverImageProperty =
            DependencyProperty.Register("UnCheckMouseOverImage", typeof(ImageSource), typeof(TriggerImageButton), new PropertyMetadata(null));

public ImageSource UncheckPressedImage
        {
            get { return (ImageSource)GetValue(UncheckPressedImageProperty); }
            set { SetValue(UncheckPressedImageProperty, value); }
        }

// Using a DependencyProperty as the backing store for UncheckPressedImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UncheckPressedImageProperty =
            DependencyProperty.Register("UncheckPressedImage", typeof(ImageSource), typeof(TriggerImageButton), new PropertyMetadata(null));

}
}



ToggleButton图片按钮的两种制作方法相关推荐

  1. Word中繁体艺术字的两种制作方法(转)

    Word中繁体艺术字的两种制作方法(转) Word里的艺术字是大家平时常用的一项功能,但有时我们可能需要制作一些繁体艺术字,而这个看似简单的要求,实现起来却不是那么容易,因为Word里的繁简转换功能对 ...

  2. OCR图片转文字两种python方法实现

    图片转文字的两种处理方法: 一种是文字识别工作都需要在网络侧完成的方式,我们称为在线识别: 另一种是不需要互联网功能的,我们称作离线识别. 在线识别方式 先看第一种,在线识别的方式.在线识别方式最大的 ...

  3. PDF文件如何转换成转图片?分享两种实现方法

    PDF文件通常是用于在不同计算机.操作系统和应用程序之间共享文档的标准格式.但有时,您可能需要将PDF文件转换为图像文件以进行其他用途.PDF文件如何转换成转图片?本文将介绍两种将PDF文件转换为图像 ...

  4. android 图片叠加xml,Android实现图片叠加效果的两种方法

    本文实例讲述了Android实现图片叠加效果的两种方法.,具体如下: 效果图: 第一种: 第二种: 第一种是通过canvas画出来的效果: public void first(View v) { // ...

  5. pytorch加载自己的图片数据集的两种方法

    目录 ImageFolder 加载数据集 使用pytorch提供的Dataset类创建自己的数据集. Dataset加载数据集 接下来我们就可以构建我们的网络架构: 训练我们的网络: 保存网络模型(这 ...

  6. android 画布叠加,Android实现图片叠加效果的两种方法

    本文实例讲述了Android实现图片叠加效果的两种方法.分享给大家供大家参考,具体如下: 效果图: 第一种: 第二种: 第一种是通过canvas画出来的效果: public void first(Vi ...

  7. qt 加载 图片旋转_QT 实现图片旋转的两种方法

    第一种方案 使用 QPixmap 的 transformed 函数来实现旋转,这个函数默认是以图片中心为旋转点,不能设置旋转的中心点,使用如下: QMatrix matrix; matrix.rota ...

  8. D3D中2D图片的绘制两种方法

    2014/09/19 (转载自:http://blog.csdn.net/rabbit729/article/details/6388703) 想要在D3D中加载2D图片可以使用如下两种方法(我只想到 ...

  9. 给TextView设置图片的两种实现方法

    有时在开发过程中,我们会有在TextView中添加图片的需求(比如下图箭头所指文字中的表情).而在xml中对Textview进行属性设置时,只能设置图片在TextView的左.右.上.下四个位置,不能 ...

最新文章

  1. 大开眼界!AI在医疗和汽车行业的11个有趣应用
  2. windows下卸载oracle11g
  3. [linux] Linux日志设置(转自抚琴煮酒)
  4. [转载]js节流与防抖,防止重复提交、防止频繁重复点击
  5. C#中使用Process调取Windows中的进程(应用程序)
  6. Leetcode 930:和相同的二元子数组
  7. 手动angular2环境搭建_详解.Net Core + Angular2 环境搭建
  8. Java事务管理之Spring+Hibernate
  9. 今晚博文视点大咖直播伴你读No.2:人工智能学习路线
  10. angularjs sill 创建项目_AngularJS快速上手,从安装到运行
  11. turbo c语言教程,C语言入门教程之 Turbo C程序的基本组成
  12. 社区团购微信小程序开发
  13. 用戶故事 vs 用例
  14. 开平方算法的C++实现
  15. 定义int数组求所有奇数的和
  16. 夏天我都冷到瑟瑟发抖-用单片机diy懒人挂脖风扇方案
  17. Chrome游览器改变SameSite设置
  18. 力争群雄:2012年度IT博客大赛100强脱颖而出
  19. 信息安全与管理2002_李付贵
  20. 映射可以多对一吗_【高中数学集合与映射】(一)整数和有理数“一样多”?...

热门文章

  1. inno setup检测安装路径是否包含中文
  2. 星际密码(编程题解)
  3. 给button按钮绑定Enter回车键
  4. 如何使用 Jenkins Pipeline 流水线优雅的部署 Kubernetes 应用
  5. [Linux]冯诺依曼体系结构
  6. 如何下载蓟州区卫星地图高清版大图
  7. python画微信表情_python画微信表情符的实例代码
  8. 小学计算机教海探航论文名字,教海探航论文.doc
  9. uni-app监听窗口尺寸变化事件和隐藏键盘
  10. 记账App Java代码_基于android的记账APP大作业项目