Skip to content

Commit 69d0066

Browse files
authored
Adding style to control helper text (#2313)
Fixes #2300
1 parent f808dd4 commit 69d0066

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

MaterialDesignThemes.UITests/WPF/TextBox/TextBoxTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,30 @@ public async Task CharacterCount_WithMaxLengthSetAndCharacterCounterVisibilityCo
258258

259259
recorder.Success();
260260
}
261+
262+
[Fact]
263+
[Description("Issue 2300")]
264+
public async Task HelperText_CanSetFontColorWithAttachedStyle()
265+
{
266+
await using var recorder = new TestRecorder(App);
267+
268+
IVisualElement grid = await LoadXaml(@"
269+
<Grid Margin=""30"">
270+
<TextBox
271+
materialDesign:HintAssist.HelperText=""Test"">
272+
<materialDesign:HintAssist.HelperTextStyle>
273+
<Style TargetType=""TextBlock"" BasedOn=""{StaticResource MaterialDesignHelperTextBlock}"">
274+
<Setter Property=""Foreground"" Value=""Red"" />
275+
</Style>
276+
</materialDesign:HintAssist.HelperTextStyle>
277+
</TextBox>
278+
</Grid>");
279+
IVisualElement textBox = await grid.GetElement("/TextBox");
280+
IVisualElement helperText = await textBox.GetElement("HelperTextTextBlock");
281+
282+
Assert.Equal(Colors.Red, await helperText.GetForegroundColor());
283+
284+
recorder.Success();
285+
}
261286
}
262287
}

MaterialDesignThemes.Wpf/HintAssist.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,17 @@ public static void SetHelperTextFontSize(DependencyObject element, double value)
120120
element.SetValue(HelperTextFontSizeProperty, value);
121121

122122
#endregion
123+
124+
#region AttachedProperty : HelperTextStyleProperty
125+
public static readonly DependencyProperty HelperTextStyleProperty
126+
= DependencyProperty.RegisterAttached("HelperTextStyle", typeof(Style), typeof(HintAssist),
127+
new PropertyMetadata(null));
128+
129+
public static Style? GetHelperTextStyle(DependencyObject element) =>
130+
(Style?)element.GetValue(HelperTextStyleProperty);
131+
public static void SetHelperTextStyle(DependencyObject element, Style? value) =>
132+
element.SetValue(HelperTextStyleProperty, value);
133+
134+
#endregion
123135
}
124136
}

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.TextBox.xaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
</Setter.Value>
3030
</Setter>
3131
</Style>
32+
33+
<Style x:Key="MaterialDesignHelperTextBlock" TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
34+
<Setter Property="FontSize" Value="{Binding Path=(wpf:HintAssist.HelperTextFontSize), RelativeSource={RelativeSource Mode=TemplatedParent}}" />
35+
<Setter Property="Opacity" Value="{Binding Path=(wpf:HintAssist.HintOpacity), RelativeSource={RelativeSource Mode=TemplatedParent}}" />
36+
<Setter Property="Text" Value="{Binding Path=(wpf:HintAssist.HelperText), RelativeSource={RelativeSource Mode=TemplatedParent}}" />
37+
</Style>
3238

3339
<Style x:Key="MaterialDesignTextBoxBase" TargetType="{x:Type TextBoxBase}">
3440
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
@@ -53,6 +59,7 @@
5359
<Setter Property="wpf:TextFieldAssist.IncludeSpellingSuggestions" Value="{Binding RelativeSource={RelativeSource Self}, Path=(SpellCheck.IsEnabled)}" />
5460
<Setter Property="wpf:TextFieldAssist.CharacterCounterStyle" Value="{StaticResource MaterialDesignCharacterCounterTextBlock}" />
5561
<Setter Property="wpf:TextFieldAssist.CharacterCounterVisibility" Value="Visible" />
62+
<Setter Property="wpf:HintAssist.HelperTextStyle" Value="{StaticResource MaterialDesignHelperTextBlock}" />
5663
<Setter Property="Template">
5764
<Setter.Value>
5865
<ControlTemplate TargetType="{x:Type TextBoxBase}">
@@ -249,9 +256,7 @@
249256
</Grid.ColumnDefinitions>
250257
<TextBlock
251258
x:Name="HelperTextTextBlock"
252-
FontSize="{TemplateBinding wpf:HintAssist.HelperTextFontSize}"
253-
Opacity="{TemplateBinding wpf:HintAssist.HintOpacity}"
254-
Text="{TemplateBinding wpf:HintAssist.HelperText}" />
259+
Style="{Binding Path=(wpf:HintAssist.HelperTextStyle), RelativeSource={RelativeSource TemplatedParent}}" />
255260
<Border Grid.Column="1" x:Name="CharacterCounterContainer">
256261
<TextBlock
257262
x:Name="CharacterCounterTextBlock"

0 commit comments

Comments
 (0)