Skip to content

Commit 0fd615b

Browse files
authored
Merge pull request #4467 from MahApps/feature/show_always_validation_hint
Add an option to always show ErrorTemplate text
2 parents 7fb6f9d + 90cc711 commit 0fd615b

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/TextExamples.xaml

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
mah:TextBoxHelper.Watermark="Custom icon style" />
175175
<TextBox Margin="{StaticResource ControlMargin}"
176176
mah:TextBoxHelper.Watermark="Number smaller than 10"
177+
mah:ValidationHelper.AlwaysShowValidationError="True"
177178
Text="{Binding IntegerGreater10Property, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" />
178179
<TextBox Margin="{StaticResource ControlMargin}"
179180
mah:TextBoxHelper.SelectAllOnFocus="True"

src/MahApps.Metro/Controls/CustomValidationPopup.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,10 @@ private bool ShouldPopupOpen()
240240
var adornedElement = this.AdornedElement;
241241
var isOpen = adornedElement is not null
242242
&& Validation.GetHasError(adornedElement)
243-
&& adornedElement.IsKeyboardFocusWithin
244-
&& this.ShowValidationErrorOnKeyboardFocus
245-
&& ValidationHelper.GetShowValidationErrorOnKeyboardFocus(adornedElement);
243+
&& (ValidationHelper.GetAlwaysShowValidationError(adornedElement)
244+
|| (adornedElement.IsKeyboardFocusWithin
245+
&& this.ShowValidationErrorOnKeyboardFocus
246+
&& ValidationHelper.GetShowValidationErrorOnKeyboardFocus(adornedElement)));
246247
return isOpen;
247248
}
248249

src/MahApps.Metro/Controls/Helper/ValidationHelper.cs

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

@@ -99,5 +99,35 @@ public static void SetShowValidationErrorOnKeyboardFocus(UIElement element, bool
9999
{
100100
element.SetValue(ShowValidationErrorOnKeyboardFocusProperty, BooleanBoxes.Box(value));
101101
}
102+
103+
/// <summary>
104+
/// Identifies the AlwaysShowValidationError attached property.
105+
/// </summary>
106+
public static readonly DependencyProperty AlwaysShowValidationErrorProperty
107+
= DependencyProperty.RegisterAttached(
108+
"AlwaysShowValidationError",
109+
typeof(bool),
110+
typeof(ValidationHelper),
111+
new PropertyMetadata(BooleanBoxes.FalseBox));
112+
113+
/// <summary>
114+
/// Gets whether the validation error text should always be shown, regardless of focus or mouse position.
115+
/// </summary>
116+
[Category(AppName.MahApps)]
117+
[AttachedPropertyBrowsableForType(typeof(UIElement))]
118+
public static bool GetAlwaysShowValidationError(UIElement element)
119+
{
120+
return (bool)element.GetValue(AlwaysShowValidationErrorProperty);
121+
}
122+
123+
/// <summary>
124+
/// Sets whether the validation error text should always be shown, regardless of focus or mouse position.
125+
/// </summary>
126+
[Category(AppName.MahApps)]
127+
[AttachedPropertyBrowsableForType(typeof(UIElement))]
128+
public static void SetAlwaysShowValidationError(UIElement element, bool value)
129+
{
130+
element.SetValue(AlwaysShowValidationErrorProperty, BooleanBoxes.Box(value));
131+
}
102132
}
103133
}

src/MahApps.Metro/Styles/Controls.ValidationError.xaml

+10-1
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,23 @@
132132
<MultiDataTrigger>
133133
<MultiDataTrigger.Conditions>
134134
<Condition Binding="{Binding ElementName=ValidationPopup, Path=CanShow, Mode=OneWay}" Value="True" />
135-
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay}" Value="True" />
136135
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.HasError), Mode=OneWay}" Value="True" />
136+
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay}" Value="True" />
137137
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(mah:ValidationHelper.ShowValidationErrorOnKeyboardFocus), Mode=OneWay}" Value="True" />
138138
<Condition Binding="{Binding ElementName=ValidationPopup, Path=ShowValidationErrorOnKeyboardFocus, Mode=OneWay}" Value="True" />
139139
</MultiDataTrigger.Conditions>
140140
<Setter TargetName="ValidationPopup" Property="IsOpen" Value="True" />
141141
</MultiDataTrigger>
142142

143+
<MultiDataTrigger>
144+
<MultiDataTrigger.Conditions>
145+
<Condition Binding="{Binding ElementName=ValidationPopup, Path=CanShow, Mode=OneWay}" Value="True" />
146+
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.HasError), Mode=OneWay}" Value="True" />
147+
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(mah:ValidationHelper.AlwaysShowValidationError), Mode=OneWay}" Value="True" />
148+
</MultiDataTrigger.Conditions>
149+
<Setter TargetName="ValidationPopup" Property="IsOpen" Value="True" />
150+
</MultiDataTrigger>
151+
143152
<MultiDataTrigger>
144153
<MultiDataTrigger.Conditions>
145154
<Condition Binding="{Binding ElementName=RedTriangle, Path=IsMouseOver, Mode=OneWay}" Value="True" />

0 commit comments

Comments
 (0)