您的位置:首页 > 其它

WPF ImageButton

2018-02-05 16:29 197 查看
最常用的图片按钮:

  有正常背景、鼠标滑过背景、鼠标按下背景、不可用背景、鼠标滑过透明度、是否是透明模式(若是则仅需正常背景即可,鼠标滑过和按下时则仅改变透明度),样式放在Generic.xaml中。

  


  示例:



public class ImageButton : Button
{
static ImageButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
}
public static readonly DependencyProperty MouseOverBackgroundProperty = DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(ImageButton), new FrameworkPropertyMetadata(null));

public Brush MouseOverBackground
{
get
{
return (Brush)this.GetValue(ImageButton.MouseOverBackgroundProperty);
}
set
{
this.SetValue(ImageButton.MouseOverBackgroundProperty, value);
}
}

public static readonly DependencyProperty MousePressedBackgroundProperty = DependencyProperty.Register("MousePressedBackground", typeof(Brush), typeof(ImageButton), new FrameworkPropertyMetadata(null));

public Brush MousePressedBackground
{
get
{
return (Brush)this.GetValue(ImageButton.MousePressedBackgroundProperty);
}
set
{
this.SetValue(ImageButton.MousePressedBackgroundProperty, value);
}
}

public static readonly DependencyProperty UnEnabledBackgroundProperty = DependencyProperty.Register("UnEnabledBackground", typeof(Brush), typeof(ImageButton), new FrameworkPropertyMetadata(null));

public Brush UnEnabledBackground
{
get
{
return (Brush)this.GetValue(ImageButton.UnEnabledBackgroundProperty);
}
set
{
this.SetValue(ImageButton.UnEnabledBackgroundProperty, value);
}
}

public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ImageButton), new FrameworkPropertyMetadata(null));

public CornerRadius CornerRadius
{
get
{
return (CornerRadius)this.GetValue(ImageButton.CornerRadiusProperty);
}
set
{
this.SetValue(ImageButton.CornerRadiusProperty, value);
}
}

public Brush MouseOverForeground
{
get { return (Brush)GetValue(MouseOverForegroundProperty); }
set { SetValue(MouseOverForegroundProperty, value); }
}

// Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty MouseOverForegroundProperty =
DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(ImageButton));

public bool IsOpacityMode
{
get { return (bool)GetValue(IsOpacityModeProperty); }
set { SetValue(IsOpacityModeProperty, value); }
}

// Using a DependencyProperty as the backing store for IsOpacityMode.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsOpacityModeProperty =
DependencyProperty.Register("IsOpacityMode", typeof(bool), typeof(ImageButton), new PropertyMetadata(false));

public double MouseOverOpacity
{
get { return (double)GetValue(MouseOverOpacityProperty); }
set { SetValue(MouseOverOpacityProperty, value); }
}

// Using a DependencyProperty as the backing store for MouseOverOpacity.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty MouseOverOpacityProperty =
DependencyProperty.Register("MouseOverOpacity", typeof(double), typeof(ImageButton), new PropertyMetadata(0.8));

}


style:

<Style TargetType="{x:Type local:ImageButton}">
<Setter Property="Background" Value="White"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="CornerRadius" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ImageButton" >
<Border x:Name="border" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding CornerRadius}"
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter Focusable="False" RecognizesAccessKey="True" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<!--透明度模式-->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsOpacityMode" Value="True"/>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Opacity" Value="{Binding MouseOverOpacity,RelativeSource={RelativeSource Mode=Self}}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsOpacityMode" Value="True"/>
<Condition Property="IsPressed" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Opacity" Value="{Binding MouseOverOpacity,RelativeSource={RelativeSource Mode=Self}}"/>
</MultiTrigger>

<!--背景图片模式-->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsOpacityMode" Value="False"/>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter  Property="Background" TargetName="border"
Value="{Binding MouseOverBackground,RelativeSource={RelativeSource TemplatedParent}}"/>
<Setter Property="Foreground" Value="{Binding MouseOverForeground,RelativeSource={RelativeSource Mode=Self}}"/>
</MultiTrigger>

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsOpacityMode" Value="False"/>
<Condition Property="IsPressed" Value="True"/>
</MultiTrigger.Conditions>
<Setter  Property="Background" TargetName="border"
Value="{Binding MousePressedBackground,RelativeSource={RelativeSource TemplatedParent}}"/>
<Setter Property="Foreground" Value="{Binding MouseOverForeground,RelativeSource={RelativeSource Mode=Self}}"/>
</MultiTrigger>

<Trigger Property="IsEnabled" Value="False">
<Setter  Property="Background" TargetName="border"
Value="{Binding UnEnabledBackground,RelativeSource={RelativeSource TemplatedParent}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>

</Style>


Generic.xaml:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Senlan.CloudPos.Toolkit">

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary  Source="/此处为项目名;component/ImageButton/ImageButtonStyle.xaml"/>
<ResourceDictionary  Source="/此处为项目名;component/WaterTextBox/WaterTextBoxStyle.xaml"/>
<ResourceDictionary  Source="/此处为项目名;component/ImageRadioButton/ImageRadioButtonStyle.xaml"/>
<ResourceDictionary  Source="/此处为项目名;component/ImageCheckBox/ImageCheckBoxStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: