I open a popup when a button is pressed some certain time using DispatcherTimer. This works fine but the popup stays open even StaysOpen property is set to false. Here's the Code:
XAML:
<Grid>
<Button x:Name="_button" Content="open" PreviewMouseDown="Button_PreviewMouseDown" PreviewMouseUp="Button_PreviewMouseUp" Width="100" Height="50"/>
<Popup x:Name="_popup" StaysOpen="False" Width="300" Height="300"/>
</Grid>
Code Behind:
public partial class MainWindow : Window {
private DispatcherTimer _dispatcherTimer;
public MainWindow() {
InitializeComponent();
}
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e) {
_dispatcherTimer.Stop();
}
private void DispatcherTimer_Tick(object sender, EventArgs e) {
_dispatcherTimer.Stop();
_popup.IsOpen = true;
}
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
_dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick);
_dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 800);
_dispatcherTimer.Start();
}
}
If I open the popup without DispatcherTimer everything works as I expect. My question is:
- Why does the popup behave like this when it is opened using DispatcherTimer?
- Is there some workaround to make this work? (the popup closes automatically when clicked outside the popup)
Thanks!