Search code examples
wpfwpf-controlspopupdelay

How Can I Open a WPF Popup with a Delay?


I simply want to open up the WPF Popup with a delay, sort of like a ToolTip.

How can I achieve this?

And, by the way, Popup.PopupAnimation = PopupAnimation.Fade ... fades in too quickly. I want at least half a second in there.


Solution

  • First off ... the credit for this answer goes to Eric Burke. He answered this very question posted in the WPF Disciples group. I thought it would be useful to put this answer out on StackOverflow too.

    Basically, you need to animate the IsOpen property of the Popup with with a DiscreteBooleanKeyFrame.

    Check out the following xaml (which can easily be pasted into Kaxaml or another loose xaml editing utility):

    <Page
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
        <ContentPresenter>
            <ContentPresenter.ContentTemplate>
                <DataTemplate>
                    <Grid>
                        <CheckBox
                            x:Name="cb"
                            Width="100"
                            Height="40"
                            Content="Hover Over Me"
                        />
                        <Popup
                            x:Name="popup"
                            Placement="Bottom"
                            PlacementTarget="{Binding ElementName=cb}"
                        >
                            <Border Width="400" Height="400" Background="Red"/>
                        </Popup>
                    </Grid>
                    <DataTemplate.Triggers>
                        <Trigger SourceName="cb" Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard x:Name="bsb">
                                    <Storyboard>
                                        <BooleanAnimationUsingKeyFrames
                                            Storyboard.TargetName="popup"
                                            Storyboard.TargetProperty="IsOpen"
                                            FillBehavior="HoldEnd"
                                        >
                                            <DiscreteBooleanKeyFrame KeyTime="0:0:0.5" Value="True"/>
                                         </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <StopStoryboard BeginStoryboardName="bsb"/>
                            </Trigger.ExitActions>
                        </Trigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ContentPresenter.ContentTemplate>
        </ContentPresenter>
    </Page>
    

    Please note that I modified his original solution slightly ... to trigger the IsOpen on mouse over versus checking the CheckBox as he had it. All in the attempt to make Popup behave a little like ToolTip.