我现在想做一个ToggleButton控件。 它有6种图片 表示 6个状态,
1 Unchecked, Normal
2 Unchecked,MouseOver
3 Unchecked, Pressed
4 Checked, Normal
5 Checked, MouseOver
6 Checked, Pressed
现在换图片时通过用Setter 更改图片的Source 但是我不知道如何既表示MouseOver同时也可以知道他是Checked的状态。不知道事件触发那里该怎么写namespace MyDemo
{
public class ImageToggleButton : ToggleButton
{
static ImageToggleButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageToggleButton), new FrameworkPropertyMetadata(typeof(ImageToggleButton))); Check_NormalStateProperty = DependencyProperty.Register("Check_NormalState", typeof(string), typeof(ImageToggleButton));
Check_MouseOverStateProperty = DependencyProperty.Register("Check_MouseOverState", typeof(string), typeof(ImageToggleButton));
Check_PressedStateProperty = DependencyProperty.Register("Check_PressedState", typeof(string), typeof(ImageToggleButton)); Uncheck_NormalStateProperty = DependencyProperty.Register("Uncheck_NormalState", typeof(string), typeof(ImageToggleButton));
Uncheck_MouseOverStateProperty = DependencyProperty.Register("Uncheck_MouseOverState", typeof(string), typeof(ImageToggleButton));
Uncheck_PressedStateProperty = DependencyProperty.Register("Uncheck_PressedState", typeof(string), typeof(ImageToggleButton));
} public string Check_NormalState
{
get { return (string)GetValue(Check_NormalStateProperty); }
set { SetValue(Check_NormalStateProperty, value); }
} public string Check_MouseOverState
{
get { return (string)GetValue(Check_MouseOverStateProperty); }
set { SetValue(Check_MouseOverStateProperty, value); }
} public string Check_PressedState
{
get { return (string)GetValue(Check_PressedStateProperty); }
set { SetValue(Check_PressedStateProperty, value); }
} public string Uncheck_NormalState
{
get { return (string)GetValue(Uncheck_NormalStateProperty); }
set { SetValue(Uncheck_NormalStateProperty, value); }
} public string Uncheck_MouseOverState
{
get { return (string)GetValue(Uncheck_MouseOverStateProperty); }
set { SetValue(Uncheck_MouseOverStateProperty, value); }
} public string Uncheck_PressedState
{
get { return (string)GetValue(Uncheck_PressedStateProperty); }
set { SetValue(Uncheck_PressedStateProperty, value); }
} public static DependencyProperty Check_NormalStateProperty;
public static DependencyProperty Check_MouseOverStateProperty;
public static DependencyProperty Check_PressedStateProperty; public static DependencyProperty Uncheck_NormalStateProperty;
public static DependencyProperty Uncheck_MouseOverStateProperty;
public static DependencyProperty Uncheck_PressedStateProperty;
}
}
<Style TargetType="{x:Type local:ImageToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ImageToggleButton}">
<Border>
<Image Name="imgground" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ImageToggleButton.Uncheck_NormalState)}" Stretch="Fill"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="imgground" Property="Source" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ImageToggleButton.Check_NormalState)}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True" >
<Setter TargetName="imgground" Property="Source" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ImageToggleButton.Check_MouseOverState)}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="imgground" Property="Source" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ImageToggleButton.Check_PressedState)}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
不知道<ControlTemplate.Triggers>里面该怎么写
1 Unchecked, Normal
2 Unchecked,MouseOver
3 Unchecked, Pressed
4 Checked, Normal
5 Checked, MouseOver
6 Checked, Pressed
现在换图片时通过用Setter 更改图片的Source 但是我不知道如何既表示MouseOver同时也可以知道他是Checked的状态。不知道事件触发那里该怎么写namespace MyDemo
{
public class ImageToggleButton : ToggleButton
{
static ImageToggleButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageToggleButton), new FrameworkPropertyMetadata(typeof(ImageToggleButton))); Check_NormalStateProperty = DependencyProperty.Register("Check_NormalState", typeof(string), typeof(ImageToggleButton));
Check_MouseOverStateProperty = DependencyProperty.Register("Check_MouseOverState", typeof(string), typeof(ImageToggleButton));
Check_PressedStateProperty = DependencyProperty.Register("Check_PressedState", typeof(string), typeof(ImageToggleButton)); Uncheck_NormalStateProperty = DependencyProperty.Register("Uncheck_NormalState", typeof(string), typeof(ImageToggleButton));
Uncheck_MouseOverStateProperty = DependencyProperty.Register("Uncheck_MouseOverState", typeof(string), typeof(ImageToggleButton));
Uncheck_PressedStateProperty = DependencyProperty.Register("Uncheck_PressedState", typeof(string), typeof(ImageToggleButton));
} public string Check_NormalState
{
get { return (string)GetValue(Check_NormalStateProperty); }
set { SetValue(Check_NormalStateProperty, value); }
} public string Check_MouseOverState
{
get { return (string)GetValue(Check_MouseOverStateProperty); }
set { SetValue(Check_MouseOverStateProperty, value); }
} public string Check_PressedState
{
get { return (string)GetValue(Check_PressedStateProperty); }
set { SetValue(Check_PressedStateProperty, value); }
} public string Uncheck_NormalState
{
get { return (string)GetValue(Uncheck_NormalStateProperty); }
set { SetValue(Uncheck_NormalStateProperty, value); }
} public string Uncheck_MouseOverState
{
get { return (string)GetValue(Uncheck_MouseOverStateProperty); }
set { SetValue(Uncheck_MouseOverStateProperty, value); }
} public string Uncheck_PressedState
{
get { return (string)GetValue(Uncheck_PressedStateProperty); }
set { SetValue(Uncheck_PressedStateProperty, value); }
} public static DependencyProperty Check_NormalStateProperty;
public static DependencyProperty Check_MouseOverStateProperty;
public static DependencyProperty Check_PressedStateProperty; public static DependencyProperty Uncheck_NormalStateProperty;
public static DependencyProperty Uncheck_MouseOverStateProperty;
public static DependencyProperty Uncheck_PressedStateProperty;
}
}
<Style TargetType="{x:Type local:ImageToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ImageToggleButton}">
<Border>
<Image Name="imgground" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ImageToggleButton.Uncheck_NormalState)}" Stretch="Fill"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="imgground" Property="Source" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ImageToggleButton.Check_NormalState)}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True" >
<Setter TargetName="imgground" Property="Source" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ImageToggleButton.Check_MouseOverState)}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="imgground" Property="Source" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ImageToggleButton.Check_PressedState)}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
不知道<ControlTemplate.Triggers>里面该怎么写
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货