WPF:动画不起作用在更改时IsEnabled为{False或True}

时间:2017-10-30 06:56:19

标签: c# wpf animation

显示默认的“网格”。

在第一步和“Hide_MouseUp”中,动画有效。

然后决不会回应IsEnabled。

请帮我解决这个问题。

I am waiting for your warm response :)

式:

<Style x:Key="ShowHideVisibilityStyle" TargetType="{x:Type Grid}">
    <Setter Property="Opacity" Value="1"></Setter>
    <Setter Property="Margin" Value="0,50,0,0"></Setter>
    <Setter Property="IsEnabled" Value="True" />
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation
                            Storyboard.TargetProperty="Opacity"
                            From="0" To="1" Duration="0:0:0.5" />
                        <ThicknessAnimation   
                            Storyboard.TargetProperty="Margin"
                            From="-600,50,0,0"
                            To="0,50,0,0" 
                            Duration="0:0:0.1"
                        />
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation
                            Storyboard.TargetProperty="Opacity"
                            From="1" To="0" Duration="0:0:0.5" />
                        <ThicknessAnimation   
                            Storyboard.TargetProperty="Margin"
                            BeginTime="0:0:0.5"
                            To="-600,50,0,0" 
                            Duration="0:0:0" />
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>
</Style>

XAML:

<Grid x:Name="SettingPage" Width="290" Style="{DynamicResource ShowHideVisibilityStyle}">
    <!-- code -->
</Grid>

代码测试更改状态IsEnabled为{False或True}

private void Hide_MouseUp(object sender, MouseButtonEventArgs e){
    SettingPage.IsEnabled = false;
}
private void Show_MouseUp(object sender, MouseButtonEventArgs e){
    SettingPage.IsEnabled = true;
}

2 个答案:

答案 0 :(得分:1)

  

嗨,我找到了解决办法:)

在一个属性“Trigger.EnterActions”和“Trigger.ExitActions”上使用两个触发器,而不是使用两个单独的Triggers

请原谅我的英语不好:D

代码:

<Style.Triggers>
    <Trigger Property="IsEnabled" Value="True">
        <Trigger.EnterActions>
            <BeginStoryboard>
                <Storyboard>
                   <!-- Animation -->
                </Storyboard>
            </BeginStoryboard>
        </Trigger.EnterActions>

        <Trigger.ExitActions>
            <BeginStoryboard>
                <Storyboard>
                    <!-- Animation -->
                </Storyboard>
            </BeginStoryboard>
        </Trigger.ExitActions>
    </Trigger>
</Style.Triggers>

答案 1 :(得分:1)

这是一个简单的修复。只需将“启用”动画移动到ExitTrigger

即可
<Style.Triggers>
    <Trigger Property="IsEnabled" Value="False">
        <Trigger.EnterActions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                        Storyboard.TargetProperty="Opacity"
                        From="1" To="0" Duration="0:0:0.5" />
                    <ThicknessAnimation   
                        Storyboard.TargetProperty="Margin"
                        BeginTime="0:0:0.5"
                        To="-600,50,0,0" 
                        Duration="0:0:0" />
                </Storyboard>
            </BeginStoryboard>
        </Trigger.EnterActions>
        <Trigger.ExitActions>
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation
                        Storyboard.TargetProperty="Opacity"
                        From="0" To="1" Duration="0:0:0.5" />
                    <ThicknessAnimation   
                        Storyboard.TargetProperty="Margin"
                        From="-600,50,0,0"
                        To="0,50,0,0" 
                        Duration="0:0:0.1"
                    />
                </Storyboard>
            </BeginStoryboard>
        </Trigger.ExitActions>
    </Trigger>
</Style.Triggers>