Seems I always get stumped by what should be the simplest of things...
I have a UserControl with a custom bool DependencyProperty 'GpoModule'.
The default is set to false.
I have a FadeIn animation triggered when 'GpoModule' = true and a FadeOut animation triggered when 'GpoModule' = false.
My problem is, When the control is loaded, the FadeOut animation is triggered, I want the animation to be triggered only when the property is changed (or at least not on the default value when loaded). I suspect I may have to define an event trigger of some kind, but I'm hoping to avoid that.
DependencyProperty:
public bool GpoModule {
get { return (bool)GetValue(GpoModuleProperty); }
set { SetValue(GpoModuleProperty, value); }
}
public static readonly DependencyProperty GpoModuleProperty =
DependencyProperty.Register("GpoModule", typeof(bool),
typeof(ModuleIndicator), new UIPropertyMetadata(false));
Animation:
<UserControl.Resources>
<Duration x:Key="D">0:0:0.6</Duration>
<Storyboard x:Key="SbFadeOut">
<DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Width)"
From="38" To="0"
Duration="{StaticResource D}">
<DoubleAnimation.EasingFunction>
<QuadraticEase EasingMode="EaseInOut" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Height)"
From="38"
To="0"
Duration="{StaticResource D}">
<DoubleAnimation.EasingFunction>
<QuadraticEase EasingMode="EaseInOut" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetProperty="(UIElement.Opacity)"
From="1"
To="0"
Duration="{StaticResource D}">
<DoubleAnimation.EasingFunction>
<QuadraticEase EasingMode="EaseInOut" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
<Storyboard x:Key="SbFadeIn">
<DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Width)"
From="0" To="38"
Duration="{StaticResource D}">
<DoubleAnimation.EasingFunction>
<QuadraticEase EasingMode="EaseInOut" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Height)"
From="0"
To="38"
Duration="{StaticResource D}">
<DoubleAnimation.EasingFunction>
<QuadraticEase EasingMode="EaseInOut" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetProperty="(UIElement.Opacity)"
From="0"
To="1"
Duration="{StaticResource D}">
<DoubleAnimation.EasingFunction>
<QuadraticEase EasingMode="EaseInOut" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</UserControl.Resources>
Animated Element:
<Viewbox x:Name="GpoIndicator"
VerticalAlignment="Center" HorizontalAlignment="Center"
Stretch="Fill" Width="0" Height="0">
<Grid Height="38" Width="38">
<Ellipse Height="38"
Fill="{DynamicResource App.Primary.Light}"
Stroke="{DynamicResource App.Border}" />
<TextBlock Width="38"
Height="38"
Margin="0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontFamily="{DynamicResource AppFont}"
FontSize="30"
FontWeight="Bold"
Foreground="{DynamicResource ResourceKey=App.Accent.Secondary}"
Padding="0,2,0,0"
Text="G"
TextAlignment="Center"
TextWrapping="Wrap"/>
</Grid>
<Viewbox.Style>
<Style TargetType="Viewbox">
<Style.Triggers>
<DataTrigger Binding="{Binding GpoModule, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ModuleIndicator}}}"
Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="GpoIn">
<Storyboard>
<StaticResource ResourceKey="SbFadeIn" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="GpoIn" />
</DataTrigger.ExitActions>
</DataTrigger>
<DataTrigger Binding="{Binding GpoModule, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ModuleIndicator}}}"
Value="False">
<DataTrigger.EnterActions>
<BeginStoryboard Name="GpoOut">
<Storyboard>
<StaticResource ResourceKey="SbFadeOut" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="GpoOut" />
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Viewbox.Style>
</Viewbox>
Thanks!
Edit: I should have mentioned there are two other 'Indicator' objects / children - 'RedesignIndicator' and 'NovellIndicator'. Each with respective bool DependencyProperties.
I tried using multiple conditions before asking the question. My first attempt was setting a condition to 'IsLoaded' which had no effect.
My second attempt was to use another Dependency property 'AnimationLocked', with default set to true, and tried two variations on this.
One: set AnimationLocked to false on the property changed callback (this had the side effect of triggering the FadeOut animation on one of the other child objects and
Two: set AnimationLocked to false on the UserControl.Loaded event (which had no effect)