淡入隐藏的元素

时间:2013-08-30 10:36:33

标签: c# wpf windows-store-apps

我可以使用以下代码淡入/淡出

<Storyboard x:Name="EnterStoryboard">
     <FadeOutThemeAnimation Storyboard.TargetName="PauseImage" />
</Storyboard>
<Storyboard x:Name="ExitStoryboard">
     <FadeInThemeAnimation Storyboard.TargetName="PauseImage" />
</Storyboard>

褪色:

EnterStoryboard.Begin();

淡出:

ExitStoryboard.Begin();

如果元素从头开始隐藏,我怎么能淡化(我试图设置opacity=0visibility=collapsed)。

修改
基于AstiK解决方案,这是新的Storyboard s(而不是内置的FadeInThemeAnimation / FadeOutThemeAnimation

<Storyboard x:Name="EnterStoryboard">
    <DoubleAnimation BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.Opacity)" From="0" To="1" Duration="00:00:00.3"  Storyboard.TargetName="Image"/>
</Storyboard>
<Storyboard x:Name="ExitStoryboard">
    <DoubleAnimation BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.Opacity)" From="1" To="0" Duration="00:00:00.3"  Storyboard.TargetName="Image"/>
</Storyboard>

2 个答案:

答案 0 :(得分:1)

在您的原始方法中,您应该从头开始保留Opacity ="0"Visibility="Visible"。我想你正在寻找这样的东西:

<Grid Height="50" Width="100" Background="Red" Opacity="0">
        <Grid.Style>
            <Style TargetType="Grid">
                <Style.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter">
                        <BeginStoryboard>
                            <Storyboard>
                                <Storyboard  >
                                    <DoubleAnimation BeginTime="00:00:00"  Storyboard.TargetProperty="(UIElement.Opacity)" From="0" To="1" Duration="00:00:03"/>
                                </Storyboard>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
    </Grid>

答案 1 :(得分:0)

使用它:

<Storyboard  x:Name="EnterStoryboard">
       <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="PauseImage">
            <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Visible</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
        </ObjectAnimationUsingKeyFrames>
        <FadeOutThemeAnimation Storyboard.TargetName="PauseImage" />

设置为以ExitStoryboard

的相同方式折叠