0

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!

Fresh Saint
  • 139
  • 2
  • 12
Jani
  • 1
  • 2
  • 1
    You open the popup manually by `_popup.IsOpen = true;` so what is the issue? It will be closed whenever lost focus is fired on popup. That's how it works without timer in place as well. – Rohit Vats Jan 28 '14 at 07:34
  • And where are you setting `_popup.IsOpen` to `false`? – Sheridan Jan 28 '14 at 09:36
  • I expect it to close automatically because StaysOpen = false, when I click outside the popup. And it does when it is opened without DispatcherTimer. – Jani Jan 28 '14 at 09:50
  • @Rohit Vats, I don't know did you try the code, but the problem is that it is NOT closed when lost focus is fired on popup when DispatcherTimer is used. – Jani Jan 28 '14 at 10:09
  • Yeah i tried the code and it was closing on lost focus for me. – Rohit Vats Jan 28 '14 at 10:18
  • @RohitVats, For me it closes only if I click outside the whole window. But is does not close if I click the main window of the application. I expect it to do that like it does without DispatcherTimer. I am sorry not to be specific. – Jani Jan 28 '14 at 10:23
  • So kindly post a small working sample where this issue can be produced. – Rohit Vats Jan 28 '14 at 10:42
  • @RohitVats Here are more detailed steps to reproduce this: 1. use the code above 2. open the popup by holding the button down about 1sec 3. click the button again --> This should close the popup because StaysOpen=false, right? But it does not do that (at least for me) But if I open the popup directly by Click event, the popup closes when the button is clicked again and this is the behavior i expect and want also by using DispatcherTimer. – Jani Jan 29 '14 at 05:31

0 Answers0