Skip to content

Commit e4bb760

Browse files
Valmir-Cunhapunker76
authored andcommitted
feat: Included setting "ChangeFocusOnUsernameFieldWithEnter" in LoginDialogSettings to change focus from the username field to the password field when pressing ENTER on the username field
1 parent 476c73e commit e4bb760

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/MahApps.Metro/Controls/Dialogs/LoginDialog.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ public bool RememberCheckBoxChecked
224224
set => this.SetValue(RememberCheckBoxCheckedProperty, BooleanBoxes.Box(value));
225225
}
226226

227+
/// <summary>Identifies the <see cref="ChangeFocusOnUsernameFieldWithEnterProperty"/> dependency property.</summary>
228+
public static readonly DependencyProperty ChangeFocusOnUsernameFieldWithEnterProperty
229+
= DependencyProperty.Register(nameof(ChangeFocusOnUsernameFieldWithEnter),
230+
typeof(bool),
231+
typeof(LoginDialog),
232+
new PropertyMetadata(default(bool)));
233+
234+
public bool ChangeFocusOnUsernameFieldWithEnter
235+
{
236+
get => (bool) this.GetValue(ChangeFocusOnUsernameFieldWithEnterProperty);
237+
set => this.SetValue(ChangeFocusOnUsernameFieldWithEnterProperty, value);
238+
}
239+
227240
#endregion DependencyProperties
228241

229242
#region Constructor
@@ -257,6 +270,7 @@ internal LoginDialog(MetroWindow? parentWindow, LoginDialogSettings? settings)
257270
this.SetCurrentValue(RememberCheckBoxVisibilityProperty, loginDialogSettings.RememberCheckBoxVisibility);
258271
this.SetCurrentValue(RememberCheckBoxTextProperty, loginDialogSettings.RememberCheckBoxText);
259272
this.SetCurrentValue(RememberCheckBoxCheckedProperty, loginDialogSettings.RememberCheckBoxChecked);
273+
this.SetCurrentValue(ChangeFocusOnUsernameFieldWithEnterProperty, loginDialogSettings.ChangeFocusOnUsernameFieldWithEnter);
260274
}
261275
}
262276

@@ -299,16 +313,23 @@ private void OnKeyDownHandler(object sender, KeyEventArgs e)
299313
}
300314
else if (e.Key == Key.Enter)
301315
{
302-
this.CleanUpHandlers();
303-
304-
this.tcs.TrySetResult(!ReferenceEquals(sender, this.PART_NegativeButton)
305-
? new LoginDialogData
306-
{
307-
Username = this.Username,
308-
SecurePassword = this.PART_PasswordBox?.SecurePassword,
309-
ShouldRemember = this.RememberCheckBoxChecked
310-
}
311-
: null!);
316+
if (PART_TextBox != null && PART_TextBox.IsFocused && ChangeFocusOnUsernameFieldWithEnter)
317+
{
318+
PART_PasswordBox?.Focus();
319+
}
320+
else
321+
{
322+
this.CleanUpHandlers();
323+
324+
this.tcs.TrySetResult(!ReferenceEquals(sender, this.PART_NegativeButton)
325+
? new LoginDialogData
326+
{
327+
Username = this.Username,
328+
SecurePassword = this.PART_PasswordBox?.SecurePassword,
329+
ShouldRemember = this.RememberCheckBoxChecked
330+
}
331+
: null!);
332+
}
312333

313334
e.Handled = true;
314335
}

src/MahApps.Metro/Controls/Dialogs/LoginDialogSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,7 @@ public LoginDialogSettings(MetroDialogSettings? source)
6161
public string RememberCheckBoxText { get; set; } = DefaultRememberCheckBoxText;
6262

6363
public bool RememberCheckBoxChecked { get; set; } = false;
64+
65+
public bool ChangeFocusOnUsernameFieldWithEnter { get; set; }
6466
}
6567
}

0 commit comments

Comments
 (0)