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.