Event closure is fired twice when exitButton is set. IsCancel = True

I realized when I set the exit button with the attribute IsCancel = True, the close event closes twice.

    private void exitButton_Click(object sender, RoutedEventArgs e)
    {
        // this button was set attribute IsCancel = True.
        Close();          
    }

    private void BaseWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {          
        MessageBox.Show("test"); // this message box will show twice
                                 // when you click on the exit button
        e.Cancel = true;
    }

Is this a WPF bug? Is there any workaround?

Ps: Sorry, I forgot to say that this error only occurs when your window is called from the parent window.

+5
source share
1 answer

I think I don’t see where this unexpected behavior is.

If you designated it as a button Canceland invoke .ShowDialog(), then pressing the button will close the window.

You added your own call to Close()and canceled the close, so both calls are made and the event is incremented at the same time.

, , IsCancel IsDefault XAML. .

+4

All Articles