WPF ImageButton

最新版移步至http://blog.csdn.net/oneonce/article/details/77001854


创建CustomControl

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:ExtendedButton">

    <Style TargetType="{x:Type local:ImageButton}">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="BorderThickness" Value="3"/>
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ImageButton}">
                    <Grid x:Name="ROOT_PART" HorizontalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <Border x:Name="BD_PART" Grid.Column="0" Grid.ColumnSpan="4"
                            Background="Transparent" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}"
                            CornerRadius="3"/>
                        <Image x:Name="BG_PART" Grid.Column="1"
                               Margin="{Binding BorderThickness, ElementName=BD_PART}"
                                Source="{Binding NormalImage, RelativeSource={RelativeSource TemplatedParent}}"
                                HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        <ContentPresenter Grid.Column="2"
                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                RecognizesAccessKey="True" Margin="{TemplateBinding Padding}"/>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="BG_PART" Property="Source" Value="{Binding HoverImage, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Trigger>

                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="BG_PART" Property="Source" Value="{Binding PressedImage, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="BG_PART" Property="Source" Value="{Binding DisabledImage, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

ImageButton.cs

public class ImageButton : Button
{
    static ImageButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
    }

    public ImageButton()
    {
    }

    #region Properties
 
    public static readonly DependencyProperty NormalImageProperty =
                        DependencyProperty.Register("NormalImage", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null));
    public ImageSource NormalImage
    {
        get { return (ImageSource)GetValue(NormalImageProperty); }
        set { SetValue(NormalImageProperty, value); }
    }

    public static readonly DependencyProperty HoverImageProperty =
                        DependencyProperty.Register("HoverImage", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null));
    public ImageSource HoverImage
    {
        get { return (ImageSource)GetValue(HoverImageProperty); }
        set { SetValue(HoverImageProperty, value); }
    }

    public static readonly DependencyProperty PressedImageProperty =
                        DependencyProperty.Register("PressedImage", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null));
    public ImageSource PressedImage
    {
        get { return (ImageSource)GetValue(PressedImageProperty); }
        set { SetValue(PressedImageProperty, value); }
    }

    public static readonly DependencyProperty DisabledImageProperty =
                        DependencyProperty.Register("DisabledImage", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(null));
    public ImageSource DisabledImage
    {
        get { return (ImageSource)GetValue(DisabledImageProperty); }
        set { SetValue(DisabledImageProperty, value); }
    }

    #endregion
}

使用:

<Ctrl:ImageButton Width="100" Height="40" Content="Click3" Grid.Row="2"
                BorderBrush="Green" BorderThickness="5"
                NormalImage="Images/Normal.png" 
                HoverImage="Images/Hover.png"
                PressedImage="Images/Pressed.png"
                DisabledImage="Images/Disabled.png"
                Click="ImageButton_Click"/>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OneOnce

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值