0

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>

谢谢你 ....

4

1 回答 1

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}"/>
于 2013-08-28T13:36:28.533 回答