第一种:
因为项目需要制作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));
}
}