Change button background image on hover/click using a template

前端 未结 1 1581
天命终不由人
天命终不由人 2020-12-18 15:22

I have got an application with tons and tons of buttons which I want to make a bit more fancy with hover/click effects. I was hoping one could use a styling template for thi

相关标签:
1条回答
  • 2020-12-18 15:57

    You could define attached properties for the background images for states Normal, MouseOver and Pressed (and maybe more). You would use these attached properties for the Sourceproperty of separate Image controls in a control template and modify e.g. the Image's Opacity when the VisualState changes.

    An example style:

    <Style TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="Disabled"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="mouseOverBackgroundImage" Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="1"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="pressedBackgroundImage" Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="1"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Image Name="normalBackgroundImage" Source="{TemplateBinding local:BackgroundImages.NormalBackgroundImage}"/>
                        <Image Name="mouseOverBackgroundImage" Source="{TemplateBinding local:BackgroundImages.MouseOverBackgroundImage}" Opacity="0"/>
                        <Image Name="pressedBackgroundImage" Source="{TemplateBinding local:BackgroundImages.PressedBackgroundImage}" Opacity="0"/>
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    used in a Button with the attached properties set:

    <Button local:BackgroundImages.NormalBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg"
            local:BackgroundImages.MouseOverBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
            local:BackgroundImages.PressedBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"
            Content="Hello"/>
    

    And finally the definition of those attached properties:

    public static class BackgroundImages
    {
        public static readonly DependencyProperty NormalBackgroundImageProperty =
            DependencyProperty.RegisterAttached("NormalBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));
    
        public static readonly DependencyProperty MouseOverBackgroundImageProperty =
            DependencyProperty.RegisterAttached("MouseOverBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));
    
        public static readonly DependencyProperty PressedBackgroundImageProperty =
            DependencyProperty.RegisterAttached("PressedBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));
    
        public static ImageSource GetNormalBackgroundImage(DependencyObject obj)
        {
            return (ImageSource)obj.GetValue(NormalBackgroundImageProperty);
        }
    
        public static void SetNormalBackgroundImage(DependencyObject obj, ImageSource value)
        {
            obj.SetValue(NormalBackgroundImageProperty, value);
        }
    
        public static ImageSource GetMouseOverBackgroundImage(DependencyObject obj)
        {
            return (ImageSource)obj.GetValue(MouseOverBackgroundImageProperty);
        }
    
        public static void SetMouseOverBackgroundImage(DependencyObject obj, ImageSource value)
        {
            obj.SetValue(MouseOverBackgroundImageProperty, value);
        }
    
        public static ImageSource GetPressedBackgroundImage(DependencyObject obj)
        {
            return (ImageSource)obj.GetValue(PressedBackgroundImageProperty);
        }
    
        public static void SetPressedBackgroundImage(DependencyObject obj, ImageSource value)
        {
            obj.SetValue(PressedBackgroundImageProperty, value);
        }
    }
    
    0 讨论(0)
提交回复
热议问题