Ian GM
Ian GM

Reputation: 787

WPF Style Property Trigger not firing on load

I have a style trigger on my Buttons IsEnabled property that fires and wiggles my buttons when set to True. This works fine if I disable and then re-enable the button from the code behind (MVVM) once the application has loaded and running. However, the trigger does not fire on initial load. So all my buttons that are enabled by default do not wiggle. What can I do to make the trigger work on load?

Here is my Style from my App.xaml

    <Setter Property="RenderTransformOrigin" Value="0.5,0" />
    <Setter Property="RenderTransform">
        <Setter.Value>
            <RotateTransform />
        </Setter.Value>
    </Setter>
    <Style TargetType="{x:Type Button}" x:Key="btnDefaultStyle">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard TargetProperty="RenderTransform.Angle" RepeatBehavior="Forever">
                            <DoubleAnimation From="0"  To="-5" BeginTime="0:0:0:5.00" Duration="0:0:0.05"/>
                            <DoubleAnimation From="-5" To="0"  BeginTime="0:0:0:5.05" Duration="0:0:0.05"/>
                            <DoubleAnimation From="0"  To="4"  BeginTime="0:0:0:5.10" Duration="0:0:0.05"/>
                            <DoubleAnimation From="4"  To="0"  BeginTime="0:0:0:5.15" Duration="0:0:0.05"/>
                            <DoubleAnimation From="0"  To="-3" BeginTime="0:0:0:5.20" Duration="0:0:0.05"/>
                            <DoubleAnimation From="-3" To="0"  BeginTime="0:0:0:5.25" Duration="0:0:0.05"/>
                            <DoubleAnimation From="0"  To="2"  BeginTime="0:0:0:5.30" Duration="0:0:0.05"/>
                            <DoubleAnimation From="2"  To="0"  BeginTime="0:0:0:5.35" Duration="0:0:0.05"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard TargetProperty="RenderTransform.Angle">
                            <DoubleAnimation From="0" To="0" BeginTime="0:0:0:0" Duration="0:0:0.0"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource btnDefaultStyle}" />

EventTriggers RoutedEvent, I.e. RoutedEvent="Window.Loaded" is no use as it overrides the Property IsEnable event, and therefore my disabled buttons also wiggle.

Upvotes: 0

Views: 1973

Answers (1)

AnjumSKhan
AnjumSKhan

Reputation: 9827

Use DataTrigger like below :

      <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="True">
             <DataTrigger.EnterActions>
                 <BeginStoryboard>
                       ...
                 </BeginStoryboard>
             </DataTrigger.EnterActions>
             ...
       </DataTrigger>
       <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}}" Value="False">
             ...   
       </DataTrigger>

This works at load time too.

Upvotes: 1

Related Questions