问题
In my XAML file, I have a window and I am trying to make it so the behavior is the same whether the user clicks the "X" button, or if he clicks the "Cancel" button.
My abridged code is below:
public partial class Dialog : Window
{
.
.
.
private void Window_Closing(object sender, CancelEventArgs e)
{
e.Cancel() = true; //Works as expected
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
e.Cancel() = true; //Compile error
}
}
So I realize that my problem is that RoutedEventArgs does not have a Cancel() method. Does anyone know how I can make RoutedEventArgs work more like CancelEventArgs?
回答1:
Set the IsCancel property of the button to True.
来源:https://stackoverflow.com/questions/4881760/how-would-i-make-a-cancel-button-work-like-the-x-button