我如何在TextBox上制作动画?

时间:2013-08-28 13:02:06

标签: wpf

nex代码是在文本框上制作动画的简单代码。

我需要它,但它的风格可以在一个文本框上创建动画。

我尝试但我失败了。

代码

 <TextBox  Grid.Column=" 1" Margin=" 5">
            <TextBox.Background >
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                    <GradientStop x:Name="sd1" Color="White" Offset="0"/>
                    <GradientStop x:Name="sd2" Color="#FF5F5F5F" Offset="1"/>
                </LinearGradientBrush>
            </TextBox.Background>
            <TextBox.Triggers >
                <EventTrigger RoutedEvent="GotFocus">
                    <BeginStoryboard >
                        <Storyboard >
                            <DoubleAnimation  Storyboard.TargetName="sd1" Storyboard.TargetProperty="Offset" From="0" To="1" Duration="0:0:2"></DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="LostFocus" >
                    <BeginStoryboard >
                        <Storyboard >
                            <DoubleAnimation  Storyboard.TargetName="sd1" Storyboard.TargetProperty="Offset" From="1" To="0" Duration="0:0:2"></DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBox.Triggers>
        </TextBox>
谢谢....

1 个答案:

答案 0 :(得分:2)

样式作为资源:

<Window.Resources>
    <Style x:Key="TextBoxStyle" TargetType="TextBox">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                    <GradientStop Color="White" Offset="0"/>
                    <GradientStop Color="#FF5F5F5F" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <EventTrigger RoutedEvent="GotFocus">
                <BeginStoryboard >
                    <Storyboard >
                        <DoubleAnimation Storyboard.TargetProperty="Background.GradientStops[0].Offset" From="0" To="1" Duration="0:0:2"></DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="LostFocus" >
                <BeginStoryboard >
                    <Storyboard >
                        <DoubleAnimation Storyboard.TargetProperty="Background.GradientStops[0].Offset" From="1" To="0" Duration="0:0:2"></DoubleAnimation>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

而不是文本框:

 <TextBox  Grid.Column=" 1" Margin=" 5" Style="{StaticResource TextBoxStyle}"/>