开发者

WPF - Animation to make an error message disappear

开发者 https://www.devze.com 2022-12-19 06:31 出处:网络
I have the following xaml 开发者_C百科in my window: <Border Height=\"100\" BorderBrush=\"Black\" BorderThickness=\"2\" CornerRadius=\"10\" Background=\"PaleVioletRed\" HorizontalAlignment=\"Center

I have the following xaml 开发者_C百科in my window:

<Border Height="100" BorderBrush="Black" BorderThickness="2" CornerRadius="10" Background="PaleVioletRed" HorizontalAlignment="Center" VerticalAlignment="Center">
   <TextBlock Margin="10"  HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="17" FontWeight="Bold">Error Message Here</TextBlock>
</Border>

Which basically displays this:

alt text http://xs.to/thumb-4CB2_4B69F8E6.jpg

I plan to bind it's Visibility to an error state variable so it shows when an error occurs.

But I don't want to show it for a long time. I would like it to disappear/fade after 2 seconds. Is there a way to do this via XAML? Or a nice WPF way?

Something like this psudo code logic:

when (ErrorMessage.Visibility == Visible )
{
    Wait(2000); // Wait 2 seconds
    ErrorMessage.Visibility == Collapsed;
}

but preferable done with XAML.

My instincts tell me there is a way to do this with an animation, but I am not an animation expert and could use some help.

The other option is to try and setup a timer and control it with that.


use something like this....

<EventTrigger RoutedEvent="Page.Loaded">

    <BeginStoryboard>
         <Storyboard BeginTime="0:0:1">
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="image1" Storyboard.TargetProperty="Visibility">
                    <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" />
                   </ObjectAnimationUsingKeyFrames>
              </Storyboard>

    </BeginStoryboard>
    </EventTrigger>

change the routed event to match your needs, set the BeginTime on the storyboard to 2 mins ( or whatever ), set the targetname to your border element.

0

精彩评论

暂无评论...
验证码 换一张
取 消