StatusBarItem在其DataContext更改时设置动画

时间:2014-01-03 15:38:22

标签: c# wpf xaml blend

我想在StatusBarItem文本更新后设置动画,VisualState很好,如果我在MouseEnter事件中触发它,则会播放;但是我无法在DataContextChanged活动期间播放它。

<Window.Resources>
    <Color x:Key="ColorRed">Red</Color>
</Window.Resources>

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="VisualStateGroup">
        <VisualState x:Name="Animate">
            <Storyboard AutoReverse="True">
                <ColorAnimationUsingKeyFrames Storyboard.TargetName="StatusBarItem1" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                    <EasingColorKeyFrame KeyTime="0:0:0.2" Value="{StaticResource ColorRed}" />
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<StatusBarItem x:Name="StatusBarItem1" Content="{Binding TargetNullValue='Placeholder'}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="DataContextChanged">
            <ei:GoToStateAction StateName="Animate" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</StatusBarItem>

这是我更新DataContext

的方式
...
catch (Exception ex)
{
    var s = string.Format("Building preview for asset failed: {0}", ex.Message);
    StatusBarItem1.DataContext = s;
}

如果我听了这个事件,那确实叫它:

StatusBarItem1.DataContextChanged += StatusBarItem1_DataContextChanged;
void StatusBarItem1_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
}

如果StatusBarItem文字发生变化,我该如何触发动画?

1 个答案:

答案 0 :(得分:1)

请参阅this solution,这表明这需要在代码隐藏中完成,而不能在纯XAML中完成。问题不是完全相同,但同样的解决方案应该适用于您手动启动动画。