popup auto close message about success event

asked 2020-09-09T16:48:59.877+00:00
zleug 61 Reputation points

Hi All.
In WPF c# project I would like to add popup message that will inform user about successful saved, deleted or updated event. The message should be keep on screen 3-4 seconds then automatically close. How create such message? I will appreciate for sample and detail explanation.

Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,169 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. answered 2020-09-10T03:36:57.967+00:00
    DaisyTian-1203 11,506 Reputation points

    Method 1: Set trigger for the Popup using BooleanAnimationUsingKeyFrames.

     <Popup  HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Height="200" >  
                            <Popup.Triggers>  
                                <EventTrigger RoutedEvent="MouseMove">  
                                    <BeginStoryboard>  
                                        <Storyboard>  
                                            <BooleanAnimationUsingKeyFrames Duration="0:0:3" Storyboard.TargetProperty="IsOpen">  
                                                <BooleanKeyFrameCollection>  
                                                    <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:3"/>  
                                                </BooleanKeyFrameCollection>  
                                            </BooleanAnimationUsingKeyFrames>  
                                        </Storyboard>  
                                    </BeginStoryboard>  
                                </EventTrigger>  
                            </Popup.Triggers>  
                            <TextBlock Background="LightBlue" > This is popup text</TextBlock>  
                        </Popup>  
    

    Method 2: You can add Opened event for your Popup and set a Timer for it:
    The code for the xaml:

     <Grid>  
            <Button HorizontalAlignment="Left" Click="DisplayPopup" Height="200"  Width="150" Margin="20,10,0,0">  
                <StackPanel>  
                    <TextBlock>Display Your Popup Text</TextBlock>  
                    <Popup Name="myPopup" StaysOpen="True" Opened="myPopup_Opened" >  
                        <TextBlock Name="myPopupText"  Background="LightBlue" Foreground="Blue">  
                              Popup Text  
                        </TextBlock>  
                    </Popup>  
                </StackPanel>  
            </Button>  
        </Grid>  
    

    The code to close it in cs:

    private void DisplayPopup(object sender, RoutedEventArgs e)  
            {  
                this.myPopup.IsOpen = true;  
            }  
      
            private void myPopup_Opened(object sender, EventArgs e)  
            {  
                StartCloseTimer();  
            }  
      
            private void StartCloseTimer()  
            {  
                DispatcherTimer timer = new DispatcherTimer();  
                timer.Interval = TimeSpan.FromSeconds(3d);  
                timer.Tick += TimerTick;  
                timer.Start();  
            }  
      
            private void TimerTick(object sender, EventArgs e)  
            {  
                DispatcherTimer timer = (DispatcherTimer)sender;  
                timer.Stop();  
                timer.Tick -= TimerTick;  
                this.myPopup.IsOpen = false;  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

  2. answered 2020-09-09T16:59:48.67+00:00
    Castorix31 65,271 Reputation points

    You can see for example : WPF: How to auto close a popup

    (BTW, you're talking about WPF but you set a tag windows-uwp-runtime...)

    No comments