0

我想为一个像弹出窗口这样的边框设置动画,它会在 5 秒内可见然后折叠。当边框可见时,它只会显示 5 秒然后折叠。每次边框可见时都会重复此操作。我试过了,但它只出现一次。有人可以向我解释我所犯的错误吗?

这是我实施的风格

<Grid.Resources>
        <Style x:Key="NotificationStyle" TargetType="{x:Type Border}">
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard x:Name="NotificationStoryBoard">
                            <Storyboard >
                                <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.Visibility)">
                                    <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>

                                    <DiscreteObjectKeyFrame KeyTime="00:00:05" Value="{x:Static Visibility.Collapsed}"/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>

这里是边界...

<Border Name="NotifciationPopUp" Background="White" Height="80" Width="200" BorderBrush="Gray" BorderThickness="1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Visibility="Collapsed"  Margin="10,0,0,10" Style="{StaticResource NotificationStyle}">
4

1 回答 1

0

我意识到以这种方式隐藏边框:

风格:

<Style x:Key="NotificationStyle" TargetType="{x:Type Border}">
    <Style.Triggers>
        <Trigger Property="Visibility" Value="Visible">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" From="0.0" To="1.0" Duration="0:0:1.0" />
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" BeginTime="0:0:5" From="1.0" To="0.0" Duration="0:0:1.0" />
                    </Storyboard>
                </BeginStoryboard>
           </Trigger.EnterActions>
       </Trigger>
   </Style.Triggers>
</Style>

网格:

<Grid>
    <Border Name="MyBorder" Width="100" Visibility="Hidden" Height="50" BorderThickness="2" BorderBrush="OrangeRed" Style="{StaticResource NotificationStyle}">
        <TextBlock Text="Some information" TextAlignment="Center" VerticalAlignment="Center" />
    </Border>

    <ToggleButton Name="MyButton" Content="Show border" Width="100" Height="30" VerticalAlignment="Bottom" Click="Button_Click" />
</Grid>

在代码中:

private void Button_Click(object sender, RoutedEventArgs e)
{            
    if (MyButton.IsChecked == true) 
    {
        MyBorder.Visibility = System.Windows.Visibility.Visible;
    }

    if (MyButton.IsChecked == false)
    {
        MyBorder.Visibility = System.Windows.Visibility.Hidden;
    } 
}

从他的实验中我了解到,如果该属性曾经在动画中使用过,那么只能从动画触发器中使用它。因此,我没有收到该物业的访问代码。

另外,此时控件使用了Visibility=Collapsed,它没有收到系统的消息,动画也不起作用。虽然属性“隐藏”应该接收消息,但我没有像动画那样工作,所以在示例中我使用了opacity

于 2013-04-28T15:07:23.237 回答