Skip to content

Commit

Permalink
Merge pull request #1980 from thoemmi/issue1976-escape-messagedialog
Browse files Browse the repository at this point in the history
cancel message dialog on Esc
  • Loading branch information
punker76 committed Jun 14, 2015
2 parents 18de751 + ec6596f commit 2613cf9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 21 additions & 0 deletions MahApps.Metro/Controls/Dialogs/MessageDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ internal Task<MessageDialogResult> WaitForButtonPressAsync()
RoutedEventHandler secondAuxHandler = null;
KeyEventHandler secondAuxKeyHandler = null;

KeyEventHandler escapeKeyHandler = null;

Action cleanUpHandlers = () => {
PART_NegativeButton.Click -= negativeHandler;
PART_AffirmativeButton.Click -= affirmativeHandler;
Expand All @@ -66,6 +68,8 @@ internal Task<MessageDialogResult> WaitForButtonPressAsync()
PART_AffirmativeButton.KeyDown -= affirmativeKeyHandler;
PART_FirstAuxiliaryButton.KeyDown -= firstAuxKeyHandler;
PART_SecondAuxiliaryButton.KeyDown -= secondAuxKeyHandler;
KeyDown -= escapeKeyHandler;
};

negativeKeyHandler = (sender, e) => {
Expand Down Expand Up @@ -136,6 +140,21 @@ internal Task<MessageDialogResult> WaitForButtonPressAsync()
e.Handled = true;
};

escapeKeyHandler = (sender, e) => {
if (e.Key == Key.Escape)
{
cleanUpHandlers();
tcs.TrySetResult(ButtonStyle == MessageDialogStyle.Affirmative ? MessageDialogResult.Affirmative : MessageDialogResult.Negative);
}
else if (e.Key == Key.Enter)
{
cleanUpHandlers();
tcs.TrySetResult(MessageDialogResult.Affirmative);
}
};

PART_NegativeButton.KeyDown += negativeKeyHandler;
PART_AffirmativeButton.KeyDown += affirmativeKeyHandler;
PART_FirstAuxiliaryButton.KeyDown += firstAuxKeyHandler;
Expand All @@ -146,6 +165,8 @@ internal Task<MessageDialogResult> WaitForButtonPressAsync()
PART_FirstAuxiliaryButton.Click += firstAuxHandler;
PART_SecondAuxiliaryButton.Click += secondAuxHandler;

KeyDown += escapeKeyHandler;

return tcs.Task;
}

Expand Down
3 changes: 2 additions & 1 deletion docs/release-notes/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ This is a bug fix and feature release of MahApps.Metro v1.2.0.
- Fixed MVVM Binding for `OnLabel`/`OffLabel` at `ToggleSwitch` #1867 #1945
- Fixed focus problem with `NumericUpDown` #1903 #1959
- Fixed JIT Compiler encountered an internal limitation #1919 #1971
- Fixed access keys in `CheckBox`, `RadioButton`, and `GroupBox` #1979
- Fixed access keys in `CheckBox`, `RadioButton`, and `GroupBox` #1979
- Fixed close Message dialog when pressing **Enter** or **Esc** #1976 #1980

0 comments on commit 2613cf9

Please sign in to comment.