WPF:停止Windows加载时隐藏控件的动画

时间:2016-10-08 09:08:06

标签: wpf storyboard

我有一个非常简单的淡入/淡出动画,使用数据触发器可以正常工作。我已将数据触发器绑定到bool属性,并在内部触发器中将不透明度设置为0,反之亦然。反之亦然。

现在问题是加载时bool值为false的对象,我不希望它们在加载时显示,然后动画自己隐藏。

我试图在样式设置器上将不透明度设置为0但不使用

这是按钮样式

        <Style x:Key="LocationPickerButtonStyle" TargetType="{x:Type Button}">
        <Style.Setters>
            <Setter Property="Height" Value="93"/>
            <Setter Property="Width" Value="93"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid >
                            <Image x:Name="DefaultImage" Source="something.png"/>
                           <Ellipse x:Name="HitTest" Fill="Transparent" Height="93" Width="93" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsLocationVisible}" Value="true">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard x:Name="showSelectedLocation">
                                        <Storyboard Storyboard.TargetProperty="Opacity">
                                            <DoubleAnimation Duration="0:0:1" From="0" To="1"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                                <DataTrigger.ExitActions>
                                    <StopStoryboard BeginStoryboardName="showSelectedLocation"></StopStoryboard>
                                </DataTrigger.ExitActions>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsLocationVisible}" Value="false">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard x:Name="hideSelectedLocation">
                                        <Storyboard Storyboard.TargetProperty="Opacity" >
                                            <DoubleAnimation Duration="0:0:1" From="1" To="0" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>
                                <DataTrigger.ExitActions>
                                    <StopStoryboard BeginStoryboardName="hideSelectedLocation"></StopStoryboard>
                                </DataTrigger.ExitActions>
                            </DataTrigger>
                            <Trigger Property="Opacity" Value="0">
                                <Setter Property="Visibility" Value="Collapsed"></Setter>
                            </Trigger>
                            <Trigger Property="Opacity" Value="1">
                                <Setter Property="Visibility" Value="Visible"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>

1 个答案:

答案 0 :(得分:2)

在XAML中执行以下更改:

  1. DataTriggers<ControlTemplate.Triggers>转移到Style.Triggers

  2. Opacity = 0中的Style Setter设置为每Opacity的{​​{1}}开始。

  3. 删除Button中的From = 1

  4. 完成所有更改后,您的false DataTrigger将如下所示:

    Style