如何在纯xaml中使用故事板为其设置动画后设置属性(无代码隐藏)

时间:2014-11-26 10:15:14

标签: wpf xaml animation properties setter

我正在寻找一种在纯Property动画后设置xaml的方法。

Msdn has an example of how to do that in xaml + codebehind :

XAML:

<Button
    Content="Animate and Then Set Example 1">
    <Button.Background>
    <SolidColorBrush x:Name="Button1BackgroundBrush"
        Color="Red" />
    </Button.Background>
    <Button.Triggers>
    <EventTrigger RoutedEvent="Button.Click">
        <BeginStoryboard>
        <Storyboard>
            <ColorAnimation
            Storyboard.TargetName="Button1BackgroundBrush"
            Storyboard.TargetProperty="Color"
            From="Red" To="Yellow" Duration="0:0:5"
            FillBehavior="HoldEnd"
            Completed="setButton1BackgroundBrushColor" />
        </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
    </Button.Triggers>
</Button>

C#:

private void setButton1BackgroundBrushColor(object sender, EventArgs e)
{
    Button1BackgroundBrush.Color = Colors.Blue;
}

1 个答案:

答案 0 :(得分:0)

在这种特殊情况下,您可以添加另一个ColorAnimation,并通过其BeginTime属性对其进行适当延迟,如下所示:

<Storyboard>
    <ColorAnimation
        Storyboard.TargetName="Button1BackgroundBrush"
        Storyboard.TargetProperty="Color"
        From="Red" To="Yellow" Duration="0:0:5" />
    <ColorAnimation
        Storyboard.TargetName="Button1BackgroundBrush"
        Storyboard.TargetProperty="Color"
        To="Blue" Duration="0"
        BeginTime="0:0:5" />
</Storyboard>

在更一般的情况下,根据属性类型,您可以以类似的方式使用适当的动画(如果存在)。