WPF ImageButton
2018-02-05 16:29
197 查看
最常用的图片按钮:
有正常背景、鼠标滑过背景、鼠标按下背景、不可用背景、鼠标滑过透明度、是否是透明模式(若是则仅需正常背景即可,鼠标滑过和按下时则仅改变透明度),样式放在Generic.xaml中。
示例:
style:
Generic.xaml:
有正常背景、鼠标滑过背景、鼠标按下背景、不可用背景、鼠标滑过透明度、是否是透明模式(若是则仅需正常背景即可,鼠标滑过和按下时则仅改变透明度),样式放在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>
相关文章推荐
- WPF ImageButton
- WPF ImageButton
- WPF ImageCheckButton
- WPF ImageRadioButton
- WPF ImageButton升级版
- WPF 实现不规则的ImageButton及当Disable自动变灰
- WPF控件之ImageButton
- 在WPF中,让button显示为一个Image
- WPF ImageButton
- WPF控件之DataGrid, DatePicker, Grid, GridSplitter, HyperlinkButton, Image
- 潜移默化学会WPF(绚丽篇)--热烈欢迎RadioButton,改造成功,改造成ImageButton,新版导航
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, RadioButton, AnalogClock, DigitalClock
- 无边框Button(WPF)
- android selector动态改变imageView或ImageViewButton
- wpf RadioButton控件的一个bug,onpropertychanged后会修改旧属性的值
- imagebutton 按住和松开事件
- android Button ImageButton 区别
- android-Image与Button结合的小游戏
- WPF控件库学习之Button
- GDI+ Bitmap与WPF BitmapImage的相互转换