Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting for default focused button in MessageDialog #2265

Merged
merged 2 commits into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion MahApps.Metro/Controls/Dialogs/BaseMetroDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public MetroDialogSettings()
MaximumBodyHeight = Double.NaN;

DefaultText = "";

DefaultButtonFocus = MessageDialogResult.Negative;
CancellationToken = CancellationToken.None;
}

Expand Down Expand Up @@ -398,6 +398,11 @@ public MetroDialogSettings()
/// </summary>
public double MaximumBodyHeight { get; set; }

/// <summary>
/// Gets or sets which button should be focused by default
/// </summary>
public MessageDialogResult DefaultButtonFocus { get; set; }

/// <summary>
/// Gets/sets the token to cancel the dialog.
/// </summary>
Expand Down
44 changes: 37 additions & 7 deletions MahApps.Metro/Controls/Dialogs/MessageDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,31 @@ internal Task<MessageDialogResult> WaitForButtonPressAsync()
Dispatcher.BeginInvoke(new Action(() => {
this.Focus();

var defaultButtonFocus = DialogSettings.DefaultButtonFocus;

//Ensure it's a valid option
if (!IsApplicable(defaultButtonFocus))
{
defaultButtonFocus = ButtonStyle == MessageDialogStyle.Affirmative
? MessageDialogResult.Affirmative
: MessageDialogResult.Negative;
}

//kind of acts like a selective 'IsDefault' mechanism.
switch (this.ButtonStyle)
switch (defaultButtonFocus)
{
case MessageDialogStyle.Affirmative:
case MessageDialogResult.Affirmative:
PART_AffirmativeButton.Focus();
break;

case MessageDialogStyle.AffirmativeAndNegative:
case MessageDialogStyle.AffirmativeAndNegativeAndDoubleAuxiliary:
case MessageDialogStyle.AffirmativeAndNegativeAndSingleAuxiliary:
case MessageDialogResult.Negative:
PART_NegativeButton.Focus();
break;
case MessageDialogResult.FirstAuxiliary:
PART_FirstAuxiliaryButton.Focus();
break;
case MessageDialogResult.SecondAuxiliary:
PART_SecondAuxiliaryButton.Focus();
break;
}
}));

Expand Down Expand Up @@ -282,11 +295,28 @@ public string SecondAuxiliaryButtonText
get { return (string)GetValue(SecondAuxiliaryButtonTextProperty); }
set { SetValue(SecondAuxiliaryButtonTextProperty, value); }
}

private void OnKeyCopyExecuted(object sender, ExecutedRoutedEventArgs e)
{
Clipboard.SetDataObject(Message);
}

private bool IsApplicable(MessageDialogResult value)
{
switch (value)
{
case MessageDialogResult.Affirmative:
return PART_AffirmativeButton.IsVisible;
case MessageDialogResult.Negative:
return PART_NegativeButton.IsVisible;
case MessageDialogResult.FirstAuxiliary:
return PART_FirstAuxiliaryButton.IsVisible;
case MessageDialogResult.SecondAuxiliary:
return PART_SecondAuxiliaryButton.IsVisible;
}

return false;
}
}

/// <summary>
Expand Down