See this solution, which indicates that this needs to be done on the code-behind and can't be done in pure XAML. The problem isn't exactly the same, but the same solution should work for you by manually starting the animation.
Animate a StatusBarItem when its DataContext changes
Question
I would like to animate a StatusBarItem
whenever its text is updated, the VisualState
is fine and plays if for instance I trigger it during a MouseEnter
event; however I can't get it to play during DataContextChanged
event.
<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>
This is how I update the DataContext
:
...
catch (Exception ex)
{
var s = string.Format("Building preview for asset failed: {0}", ex.Message);
StatusBarItem1.DataContext = s;
}
If I listen to the event it is indeed called :
StatusBarItem1.DataContextChanged += StatusBarItem1_DataContextChanged;
void StatusBarItem1_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
}
How can I trigger the animation whenever the StatusBarItem
text changes ?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow