WPF样式属性触发器在加载时不触发

时间:2016-05-26 15:16:45

标签: wpf xaml

我的Buttons style trigger属性上有一个IsEnabled,当设置为True时会激活并摆动我的按钮。如果我在应用程序加载并运行后禁用然后从后面的代码(MVVM)重新启用该按钮,则此方法可以正常工作。但是,trigger在初始加载时不会触发。所以我默认启用的所有按钮都不会摆动。如何使触发器在加载时工作?

以下是Style

中的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。 RoutedEvent="Window.Loaded"没有用,因为它会覆盖Property IsEnable事件,因此我的禁用按钮也会摆动。

1 个答案:

答案 0 :(得分:1)

使用如下所示的DataTrigger:

      <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>

这也适用于加载时间。