Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
844 views
in Technique[技术] by (71.8m points)

animation - WPF - Is it possible to animate a Button's background value to be equal to a LinearGradientBrush?

I want to be able to animate my button's background to be equal to a LinearGradientBrush that is referred to as "SunRise" in XAML, when the user hovers over it. However I don't know if I am even allowed to this kind of thing in WPF. Any help, explanation is appreciated.

Here is the code for the Button's style:

        <Style TargetType="{x:Type Button}" x:Key="Fourth">
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Width" Value="50"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Name="border" CornerRadius="5" Background="{TemplateBinding Background}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <EventTrigger RoutedEvent="MouseEnter">
                                <BeginStoryboard>
                                    <Storyboard Storyboard.TargetProperty="???I tried Background.Color here???" Storyboard.TargetName="border" FillBehavior="HoldEnd">
                                        <ObjectAnimationUsingKeyFrames Duration="0:0:0.5">
                                            <DiscreteObjectKeyFrame Value="{StaticResource SunRise}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                            <EventTrigger RoutedEvent="MouseLeave">
                                <BeginStoryboard>
                                    <Storyboard Storyboard.TargetProperty="???I tried Background.Color here???" Storyboard.TargetName="border" FillBehavior="HoldEnd">
                                        <ObjectAnimationUsingKeyFrames Duration="0:0:0.5">
                                            <DiscreteObjectKeyFrame Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

And here is the code for the SunRise LinearGradientBrush:

        <LinearGradientBrush x:Key="SunRise" StartPoint="0,0" EndPoint="1,1">
            <GradientStop Color="Yellow" Offset="0"/>
            <GradientStop Color="Orange" Offset="0.5"/>
            <GradientStop Color="Red" Offset="1"/>
        </LinearGradientBrush>
question from:https://stackoverflow.com/questions/65927831/wpf-is-it-possible-to-animate-a-buttons-background-value-to-be-equal-to-a-lin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...