Skip to content

Commit e9ac252

Browse files
Adopt new SmartHint in DatePicker style (#3534)
* Adopt new SmartHint in DatePicker by leveraging TextBox style * Fix/cleanup default height UI tests * Update MD3 defaults as well * Fix broken UI tests - apply same fix on TimePicker
1 parent c496f4d commit e9ac252

File tree

9 files changed

+99
-646
lines changed

9 files changed

+99
-646
lines changed

MainDemo.Wpf/FieldsLineUp.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
<DatePicker Grid.Row="1" Grid.Column="4" />
233233
<DatePicker Grid.Row="2"
234234
Grid.Column="4"
235-
materialDesign:HintAssist.IsFloating="True" />
235+
Style="{StaticResource MaterialDesignFloatingHintDatePicker}" />
236236
<DatePicker Grid.Row="3"
237237
Grid.Column="4"
238238
Style="{StaticResource MaterialDesignFilledDatePicker}" />

MaterialDesign3.Demo.Wpf/FieldsLineUp.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223
<DatePicker Grid.Row="1" Grid.Column="4" />
224224
<DatePicker Grid.Row="2"
225225
Grid.Column="4"
226-
materialDesign:HintAssist.IsFloating="True" />
226+
Style="{StaticResource MaterialDesignFloatingHintDatePicker}" />
227227
<DatePicker Grid.Row="3"
228228
Grid.Column="4"
229229
Style="{StaticResource MaterialDesignFilledDatePicker}" />

MaterialDesignThemes.UITests/WPF/DatePickers/DatePickerTests.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,24 @@ public async Task OutlinedDatePicker_RespectsActiveAndInactiveBorderThickness_Wh
117117
var datePicker = await stackPanel.GetElement<DatePicker>("/DatePicker");
118118
await datePicker.SetProperty(DatePicker.SelectedDateProperty, DateTime.Now.AddDays(1));
119119
var datePickerTextBox = await datePicker.GetElement<TextBox>("PART_TextBox");
120+
var textBoxOuterBorder = await datePickerTextBox.GetElement<Border>("OuterBorder");
120121
var button = await stackPanel.GetElement<Button>("Button");
121122

122123
// Act
123124
await button.MoveCursorTo();
124125
await Task.Delay(50); // Wait for the visual change
125-
var inactiveBorderThickness = await datePickerTextBox.GetProperty<Thickness>(Control.BorderThicknessProperty);
126+
var inactiveBorderThickness = await textBoxOuterBorder.GetBorderThickness();
126127
await datePickerTextBox.MoveCursorTo();
127128
await Task.Delay(50); // Wait for the visual change
128-
var hoverBorderThickness = await datePickerTextBox.GetProperty<Thickness>(Control.BorderThicknessProperty);
129+
var hoverBorderThickness = await textBoxOuterBorder.GetBorderThickness();
129130
await datePickerTextBox.LeftClick();
130131
await Task.Delay(50); // Wait for the visual change
131-
var focusedBorderThickness = await datePickerTextBox.GetProperty<Thickness>(Control.BorderThicknessProperty);
132+
var focusedBorderThickness = await textBoxOuterBorder.GetBorderThickness();
132133

133134
// TODO: It would be cool if a validation error could be set via XAMLTest without the need for the Binding and ValidationRules elements in the XAML above.
134135
await datePicker.SetProperty(DatePicker.SelectedDateProperty, DateTime.Now);
135136
await Task.Delay(50); // Wait for the visual change
136-
var withErrorBorderThickness = await datePickerTextBox.GetProperty<Thickness>(Control.BorderThicknessProperty);
137+
var withErrorBorderThickness = await textBoxOuterBorder.GetBorderThickness();
137138

138139
// Assert
139140
Assert.Equal(expectedInactiveBorderThickness, inactiveBorderThickness);
@@ -236,7 +237,7 @@ public async Task DatePicker_WithHintAndValidationError_RespectsPadding(string s
236237

237238
[Fact]
238239
[Description("Issue 3365")]
239-
public async Task DatePicker_WithoutOutlinedStyleAndNoCustomHintBackgroundSet_ShouldApplyDefaultBackgroundWhenFloated()
240+
public async Task DatePicker_WithOutlinedStyleAndNoCustomHintBackgroundSet_ShouldApplyDefaultBackgroundWhenFloated()
240241
{
241242
await using var recorder = new TestRecorder(App);
242243

@@ -250,19 +251,19 @@ public async Task DatePicker_WithoutOutlinedStyleAndNoCustomHintBackgroundSet_Sh
250251
""");
251252
var datePicker = await stackPanel.GetElement<DatePicker>("/DatePicker");
252253
var datePickerTextBox = await datePicker.GetElement<DatePickerTextBox>("/DatePickerTextBox");
253-
var hintBackgroundBorder = await datePicker.GetElement<Border>("HintBackgroundBorder");
254+
var hintBackgroundGrid = await datePicker.GetElement<Grid>("HintBackgroundGrid");
254255

255256
var defaultBackground = Colors.Transparent;
256257
var defaultFloatedBackground = await GetThemeColor("MaterialDesign.Brush.Background");
257258

258259
// Assert (unfocused state)
259-
Assert.Equal(defaultBackground, await hintBackgroundBorder.GetBackgroundColor());
260+
Assert.Equal(defaultBackground, await hintBackgroundGrid.GetBackgroundColor());
260261

261262
// Act
262263
await datePickerTextBox.MoveKeyboardFocus();
263264

264265
// Assert (focused state)
265-
Assert.Equal(defaultFloatedBackground, await hintBackgroundBorder.GetBackgroundColor());
266+
Assert.Equal(defaultFloatedBackground, await hintBackgroundGrid.GetBackgroundColor());
266267

267268
recorder.Success();
268269
}

MaterialDesignThemes.UITests/WPF/TextFieldDefaultHeightTests.cs

Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,37 @@ public async Task SameHeightWithDefaultStyle()
99
{
1010
await using var recorder = new TestRecorder(App);
1111

12-
// TODO: Remove controls from here as they adopt the new SmartHint approach
13-
var stackPanel = await LoadXaml<StackPanel>("""
14-
<StackPanel>
15-
<DatePicker />
16-
</StackPanel>
17-
""");
18-
19-
await GetHeight(stackPanel, "DatePicker");
20-
21-
recorder.Success();
22-
}
23-
24-
[Fact]
25-
public async Task SameHeightWithDefaultStyle_PostSmartHintRefactor()
26-
{
27-
await using var recorder = new TestRecorder(App);
28-
29-
// TODO: Add controls here as they adopt the new SmartHint approach. Once all controls have migrated, collapse into a single test with the old name.
3012
var stackPanel = await LoadXaml<StackPanel>("""
3113
<StackPanel>
3214
<TextBox />
3315
<PasswordBox />
3416
<ComboBox IsEditable="True" />
17+
<DatePicker />
3518
<materialDesign:TimePicker />
3619
</StackPanel>
3720
""");
3821

3922
double height = await GetHeight(stackPanel, "TextBox");
4023
Assert.Equal(height, await GetHeight(stackPanel, "PasswordBox"), Precision);
4124
Assert.Equal(height, await GetHeight(stackPanel, "ComboBox"), Precision);
25+
Assert.Equal(height, await GetHeight(stackPanel, "DatePicker"), Precision);
4226
Assert.Equal(height, await GetHeight(stackPanel, "TimePicker"), Precision);
4327

4428
recorder.Success();
4529
}
4630

47-
[Fact]
31+
[Fact]
4832
public async Task SameHeightWithFloatingHintStyle()
4933
{
5034
await using var recorder = new TestRecorder(App);
5135

52-
// TODO: Remove controls from here as they adopt the new SmartHint approach
53-
var stackPanel = await LoadXaml<StackPanel>("""
54-
<StackPanel>
55-
<DatePicker Style="{StaticResource MaterialDesignFloatingHintDatePicker}" materialDesign:HintAssist.Hint="Hint" />
56-
</StackPanel>
57-
""");
58-
59-
await GetHeight(stackPanel, "DatePicker");
60-
61-
recorder.Success();
62-
}
63-
64-
[Fact]
65-
public async Task SameHeightWithFloatingHintStyle_PostSmartHintRefactor()
66-
{
67-
await using var recorder = new TestRecorder(App);
68-
69-
// TODO: Add controls here as they adopt the new SmartHint approach. Once all controls have migrated, collapse into a single test with the old name.
7036
var stackPanel = await LoadXaml<StackPanel>("""
7137
<StackPanel>
7238
<TextBox Style="{StaticResource MaterialDesignFloatingHintTextBox}" materialDesign:HintAssist.Hint="Hint" />
7339
<PasswordBox Style="{StaticResource MaterialDesignFloatingHintPasswordBox}" materialDesign:HintAssist.Hint="Hint" />
7440
<PasswordBox x:Name="RevealPasswordBox" Style="{StaticResource MaterialDesignFloatingHintRevealPasswordBox}" materialDesign:HintAssist.Hint="Hint" />
7541
<ComboBox IsEditable="True" Style="{StaticResource MaterialDesignFloatingHintComboBox}" materialDesign:HintAssist.Hint="Hint" />
42+
<DatePicker Style="{StaticResource MaterialDesignFloatingHintDatePicker}" materialDesign:HintAssist.Hint="Hint" />
7643
<materialDesign:TimePicker Style="{StaticResource MaterialDesignFloatingHintTimePicker}" materialDesign:HintAssist.Hint="Hint" />
7744
</StackPanel>
7845
""");
@@ -81,6 +48,7 @@ public async Task SameHeightWithFloatingHintStyle_PostSmartHintRefactor()
8148
Assert.Equal(height, await GetHeight(stackPanel, "PasswordBox"), Precision);
8249
Assert.Equal(height, await GetHeight(stackPanel, "PasswordBox", "RevealPasswordBox"), Precision);
8350
Assert.Equal(height, await GetHeight(stackPanel, "ComboBox"), Precision);
51+
Assert.Equal(height, await GetHeight(stackPanel, "DatePicker"), Precision);
8452
Assert.Equal(height, await GetHeight(stackPanel, "TimePicker"), Precision);
8553

8654
recorder.Success();
@@ -91,30 +59,13 @@ public async Task SameHeightWithFilledStyle()
9159
{
9260
await using var recorder = new TestRecorder(App);
9361

94-
// TODO: Remove controls from here as they adopt the new SmartHint approach
95-
var stackPanel = await LoadXaml<StackPanel>("""
96-
<StackPanel>
97-
<DatePicker Style="{StaticResource MaterialDesignFilledDatePicker}" materialDesign:HintAssist.Hint="Hint" />
98-
</StackPanel>
99-
""");
100-
101-
await GetHeight(stackPanel, "DatePicker");
102-
103-
recorder.Success();
104-
}
105-
106-
[Fact]
107-
public async Task SameHeightWithFilledStyle_PostSmartHintRefactor()
108-
{
109-
await using var recorder = new TestRecorder(App);
110-
111-
// TODO: Add controls here as they adopt the new SmartHint approach. Once all controls have migrated, collapse into a single test with the old name.
11262
var stackPanel = await LoadXaml<StackPanel>("""
11363
<StackPanel>
11464
<TextBox Style="{StaticResource MaterialDesignFilledTextBox}" materialDesign:HintAssist.Hint="Hint" />
11565
<PasswordBox Style="{StaticResource MaterialDesignFilledPasswordBox}" materialDesign:HintAssist.Hint="Hint" />
11666
<PasswordBox x:Name="RevealPasswordBox" Style="{StaticResource MaterialDesignFilledRevealPasswordBox}" materialDesign:HintAssist.Hint="Hint" />
11767
<ComboBox IsEditable="True" Style="{StaticResource MaterialDesignFilledComboBox}" materialDesign:HintAssist.Hint="Hint" />
68+
<DatePicker Style="{StaticResource MaterialDesignFilledDatePicker}" materialDesign:HintAssist.Hint="Hint" />
11869
<materialDesign:TimePicker Style="{StaticResource MaterialDesignFilledTimePicker}" materialDesign:HintAssist.Hint="Hint" />
11970
</StackPanel>
12071
""");
@@ -123,6 +74,7 @@ public async Task SameHeightWithFilledStyle_PostSmartHintRefactor()
12374
Assert.Equal(height, await GetHeight(stackPanel, "PasswordBox"), Precision);
12475
Assert.Equal(height, await GetHeight(stackPanel, "PasswordBox", "RevealPasswordBox"), Precision);
12576
Assert.Equal(height, await GetHeight(stackPanel, "ComboBox"), Precision);
77+
Assert.Equal(height, await GetHeight(stackPanel, "DatePicker"), Precision);
12678
Assert.Equal(height, await GetHeight(stackPanel, "TimePicker"), Precision);
12779

12880
recorder.Success();
@@ -133,30 +85,13 @@ public async Task SameHeightWithOutlinedStyle()
13385
{
13486
await using var recorder = new TestRecorder(App);
13587

136-
// TODO: Remove controls from here as they adopt the new SmartHint approach
137-
var stackPanel = await LoadXaml<StackPanel>("""
138-
<StackPanel>
139-
<DatePicker Style="{StaticResource MaterialDesignOutlinedDatePicker}" materialDesign:HintAssist.Hint="Hint" />
140-
</StackPanel>
141-
""");
142-
143-
await GetHeight(stackPanel, "DatePicker");
144-
145-
recorder.Success();
146-
}
147-
148-
[Fact]
149-
public async Task SameHeightWithOutlinedStyle_PostSmartHintRefactor()
150-
{
151-
await using var recorder = new TestRecorder(App);
152-
153-
// TODO: Add controls here as they adopt the new SmartHint approach. Once all controls have migrated, collapse into a single test with the old name.
15488
var stackPanel = await LoadXaml<StackPanel>("""
15589
<StackPanel>
15690
<TextBox Style="{StaticResource MaterialDesignOutlinedTextBox}" materialDesign:HintAssist.Hint="Hint" />
15791
<PasswordBox Style="{StaticResource MaterialDesignOutlinedPasswordBox}" materialDesign:HintAssist.Hint="Hint" />
15892
<PasswordBox x:Name="RevealPasswordBox" Style="{StaticResource MaterialDesignOutlinedRevealPasswordBox}" materialDesign:HintAssist.Hint="Hint" />
15993
<ComboBox IsEditable="True" Style="{StaticResource MaterialDesignOutlinedComboBox}" />
94+
<DatePicker Style="{StaticResource MaterialDesignOutlinedDatePicker}" materialDesign:HintAssist.Hint="Hint" />
16095
<materialDesign:TimePicker Style="{StaticResource MaterialDesignOutlinedTimePicker}" materialDesign:HintAssist.Hint="Hint" />
16196
</StackPanel>
16297
""");
@@ -165,6 +100,7 @@ public async Task SameHeightWithOutlinedStyle_PostSmartHintRefactor()
165100
Assert.Equal(height, await GetHeight(stackPanel, "PasswordBox"), Precision);
166101
Assert.Equal(height, await GetHeight(stackPanel, "PasswordBox", "RevealPasswordBox"), Precision);
167102
Assert.Equal(height, await GetHeight(stackPanel, "ComboBox"), Precision);
103+
Assert.Equal(height, await GetHeight(stackPanel, "DatePicker"), Precision);
168104
Assert.Equal(height, await GetHeight(stackPanel, "TimePicker"), Precision);
169105

170106
recorder.Success();

MaterialDesignThemes.UITests/WPF/TimePickers/TimePickerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ public async Task TimePicker_WithClearButton_ClearButtonClearsUncommittedText()
544544

545545
[Fact]
546546
[Description("Issue 3365")]
547-
public async Task TimePicker_WithoutOutlinedStyleAndNoCustomHintBackgroundSet_ShouldApplyDefaultBackgroundWhenFloated()
547+
public async Task TimePicker_WithOutlinedStyleAndNoCustomHintBackgroundSet_ShouldApplyDefaultBackgroundWhenFloated()
548548
{
549549
await using var recorder = new TestRecorder(App);
550550

MaterialDesignThemes.Wpf/Internal/ClearText.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,19 @@ static void OnClearCommand(object sender, ExecutedRoutedEventArgs e)
3737
{
3838
switch (sender)
3939
{
40-
case DatePicker datePicker:
41-
datePicker.SetCurrentValue(DatePicker.SelectedDateProperty, null);
42-
datePicker.Text = string.Empty; // Clears the text in the DatePickerTextBox which could contain uncommitted text
40+
case DatePickerTextBox datePickerTextBox:
41+
if (datePickerTextBox.GetVisualAncestry().OfType<DatePicker>().FirstOrDefault() is { } datePicker)
42+
{
43+
datePicker.SetCurrentValue(DatePicker.SelectedDateProperty, null);
44+
datePicker.Text = string.Empty; // Clears the text in the DatePickerTextBox which could contain uncommitted text
45+
}
4346
break;
44-
case TimePicker timePicker:
45-
timePicker.SetCurrentValue(TimePicker.SelectedTimeProperty, null);
46-
timePicker.Clear(); // Clears the text in the TimePickerTextBox which could contain uncommitted text
47+
case TimePickerTextBox timePickerTextBox:
48+
if (timePickerTextBox.GetVisualAncestry().OfType<TimePicker>().FirstOrDefault() is { } timePicker)
49+
{
50+
timePicker.SetCurrentValue(TimePicker.SelectedTimeProperty, null);
51+
timePicker.Clear(); // Clears the text in the TimePickerTextBox which could contain uncommitted text
52+
}
4753
break;
4854
case TextBox textBox:
4955
textBox.SetCurrentValue(TextBox.TextProperty, null);

MaterialDesignThemes.Wpf/Themes/Generic.xaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@
3939
<Style TargetType="{x:Type local:Badged}" BasedOn="{StaticResource MaterialDesignBadge}" />
4040
<Style TargetType="{x:Type local:Card}" BasedOn="{StaticResource MaterialDesignElevatedCard}" />
4141
<Style TargetType="{x:Type local:PopupBox}" BasedOn="{StaticResource MaterialDesignPopupBox}" />
42-
<Style TargetType="{x:Type local:TimePicker}" BasedOn="{StaticResource MaterialDesignTimePicker}">
43-
<Style.Resources>
44-
<Style x:Key="NestedTextBoxStyle" TargetType="wpf:TimePickerTextBox" BasedOn="{StaticResource MaterialDesignTextBox}" />
45-
</Style.Resources>
46-
</Style>
42+
<Style TargetType="{x:Type local:TimePicker}" BasedOn="{StaticResource MaterialDesignTimePicker}" />
4743
<Style TargetType="{x:Type local:AutoSuggestBox}" BasedOn="{StaticResource MaterialDesignAutoSuggestBox}" />
4844
<Style TargetType="{x:Type local:SplitButton}" BasedOn="{StaticResource MaterialDesignRaisedSplitButton}" />
4945

0 commit comments

Comments
 (0)