Skip to content

Commit 6ae4ff6

Browse files
Introduce AP to control IsTabStop of the reveal button in PasswordBox (reveal style) (#3800)
* Add failing unit test * Add AP allowing control of IsTabStop of the reveal button * Fix test by applying new AP * Removing the inherits since it does not appear to be needed. --------- Co-authored-by: Kevin Bost <[email protected]>
1 parent 5a93b0b commit 6ae4ff6

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/MaterialDesignThemes.Wpf/PasswordBoxAssist.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public static class PasswordBoxAssist
1919
public static void SetIsPasswordRevealed(DependencyObject element, bool value) => element.SetValue(IsPasswordRevealedProperty, value);
2020
public static bool GetIsPasswordRevealed(DependencyObject element) => (bool)element.GetValue(IsPasswordRevealedProperty);
2121

22+
public static readonly DependencyProperty IsRevealButtonTabStopProperty = DependencyProperty.RegisterAttached(
23+
"IsRevealButtonTabStop", typeof(bool), typeof(PasswordBoxAssist), new PropertyMetadata(true));
24+
public static void SetIsRevealButtonTabStop(DependencyObject element, bool value) => element.SetValue(IsRevealButtonTabStopProperty, value);
25+
public static bool GetIsRevealButtonTabStop(DependencyObject element) => (bool)element.GetValue(IsRevealButtonTabStopProperty);
26+
2227
public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached(
2328
"Password", typeof(string), typeof(PasswordBoxAssist), new FrameworkPropertyMetadata(null, HandlePasswordChanged) { BindsTwoWayByDefault = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.LostFocus });
2429
public static void SetPassword(DependencyObject element, string value) => element.SetValue(PasswordProperty, value);

src/MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.PasswordBox.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@
884884
<ToggleButton x:Name="RevealPasswordButton"
885885
Grid.Column="4"
886886
Height="Auto"
887+
IsTabStop="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:PasswordBoxAssist.IsRevealButtonTabStop)}"
887888
Opacity="{TemplateBinding wpf:HintAssist.HintOpacity}"
888889
Padding="2,0,0,0"
889890
VerticalAlignment="Center"

tests/MaterialDesignThemes.UITests/WPF/PasswordBoxes/PasswordBoxTests.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.ComponentModel;
1+
using System.ComponentModel;
22
using MaterialDesignThemes.UITests.Samples.PasswordBox;
33
using MaterialDesignThemes.UITests.WPF.TextBoxes;
44

@@ -333,4 +333,35 @@ public async Task PasswordBox_WithRevealedPassword_RespectsKeyboardTabNavigation
333333

334334
recorder.Success();
335335
}
336+
337+
[Fact]
338+
[Description("Issue 3799")]
339+
public async Task PasswordBox_WithRevealButtonIsTabStopSetToFalse_RespectsKeyboardTabNavigation()
340+
{
341+
await using var recorder = new TestRecorder(App);
342+
343+
var stackPanel = await LoadXaml<StackPanel>("""
344+
<StackPanel Orientation="Vertical">
345+
<PasswordBox x:Name="PasswordBox" Width="200"
346+
materialDesign:PasswordBoxAssist.IsRevealButtonTabStop="False"
347+
Style="{StaticResource MaterialDesignFilledRevealPasswordBox}" />
348+
<TextBox x:Name="TextBox" Width="200" />
349+
</StackPanel>
350+
""");
351+
352+
var passwordBox = await stackPanel.GetElement<PasswordBox>("PasswordBox");
353+
var textBox = await stackPanel.GetElement<TextBox>("TextBox");
354+
355+
// Assert Tab forward
356+
await passwordBox.MoveKeyboardFocus();
357+
Assert.True(await passwordBox.GetIsKeyboardFocused());
358+
await passwordBox.SendKeyboardInput($"{Key.Tab}");
359+
Assert.True(await textBox.GetIsKeyboardFocused());
360+
361+
// Assert Tab backwards
362+
await textBox.SendKeyboardInput($"{ModifierKeys.Shift}{Key.Tab}{ModifierKeys.None}");
363+
Assert.True(await passwordBox.GetIsKeyboardFocused());
364+
365+
recorder.Success();
366+
}
336367
}

0 commit comments

Comments
 (0)