C# – WPF Cancel Button

buttoncvisual studio 2010wpf

I am working on a WPF Application. One of my Windows has a Button called "Cancel" with its IsCancel=true. I need to show a message box with Yes/No when the user clicks Cancel or presses ESCAPE Key. If the user click Yes, the Window should continue closing but if the user clicks No, it should not close the form but continue the regular operation with the Window opened. How can I do so? Please help. Thanks in advance.

Best Answer

this will help you

void Window_Closing(object sender, CancelEventArgs e)
{
     MessageBoxResult result = MessageBox.Show(
            "msg", 
            "title", 
            MessageBoxButton.YesNo, 
            MessageBoxImage.Warning);
        if (result == MessageBoxResult.No)
        {
            // If user doesn't want to close, cancel closure
            e.Cancel = true;
        }        
}