简体   繁体   中英

WPF Control Visibility

I have a WPF application and I placed a dock panel over a grid then by default dock panel is Hidden. In the grid I have a button that when I click it the visibility of the dock panel is Visible and I want the dock panel Hidden after 2 seconds. How do I do this?

Try like this

<Window x:Class="WpfApplication30.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Storyboard x:Key="ShowDock">
            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="dock" Storyboard.TargetProperty="(UIElement.Visibility)">
                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}"/>
                <DiscreteObjectKeyFrame KeyTime="00:00:2" Value="{x:Static Visibility.Collapsed}"/>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>

    <Grid>
    <Grid>        
        <Button Content="show" Height="30" Width="100">
                <Button.Triggers>
                    <EventTrigger RoutedEvent="Button.Click">
                        <BeginStoryboard Storyboard="{StaticResource ShowDock}"/>
                    </EventTrigger>
                </Button.Triggers>
            </Button>
    </Grid>
        <DockPanel x:Name="dock" Background="Pink" Visibility="Collapsed"/>
    </Grid>
</Window>

I'll assume you want the dock panel to be some sort of popup-and-disappear messagebox:

You could create a storyboard with two animations. The first one an animation that changes the visibility of the dock panel to visible and the second to hidden BUT wit a start time of 2 seconds relative to the start of the storyboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM