WPF ImageButton

最常用的图片按钮:
  有正常背景、鼠标滑过背景、鼠标按下背景、不可用背景、鼠标滑过透明度、是否是透明模式(若是则仅需正常背景即可,鼠标滑过和按下时则仅改变透明度),样式放在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>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值