Skip to content

Commit

Permalink
Merge pull request #4467 from MahApps/feature/show_always_validation_…
Browse files Browse the repository at this point in the history
…hint

Add an option to always show ErrorTemplate text
  • Loading branch information
punker76 authored Feb 26, 2024
2 parents 7fb6f9d + 90cc711 commit 0fd615b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
mah:TextBoxHelper.Watermark="Custom icon style" />
<TextBox Margin="{StaticResource ControlMargin}"
mah:TextBoxHelper.Watermark="Number smaller than 10"
mah:ValidationHelper.AlwaysShowValidationError="True"
Text="{Binding IntegerGreater10Property, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" />
<TextBox Margin="{StaticResource ControlMargin}"
mah:TextBoxHelper.SelectAllOnFocus="True"
Expand Down
7 changes: 4 additions & 3 deletions src/MahApps.Metro/Controls/CustomValidationPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,10 @@ private bool ShouldPopupOpen()
var adornedElement = this.AdornedElement;
var isOpen = adornedElement is not null
&& Validation.GetHasError(adornedElement)
&& adornedElement.IsKeyboardFocusWithin
&& this.ShowValidationErrorOnKeyboardFocus
&& ValidationHelper.GetShowValidationErrorOnKeyboardFocus(adornedElement);
&& (ValidationHelper.GetAlwaysShowValidationError(adornedElement)
|| (adornedElement.IsKeyboardFocusWithin
&& this.ShowValidationErrorOnKeyboardFocus
&& ValidationHelper.GetShowValidationErrorOnKeyboardFocus(adornedElement)));
return isOpen;
}

Expand Down
32 changes: 31 additions & 1 deletion src/MahApps.Metro/Controls/Helper/ValidationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -99,5 +99,35 @@ public static void SetShowValidationErrorOnKeyboardFocus(UIElement element, bool
{
element.SetValue(ShowValidationErrorOnKeyboardFocusProperty, BooleanBoxes.Box(value));
}

/// <summary>
/// Identifies the AlwaysShowValidationError attached property.
/// </summary>
public static readonly DependencyProperty AlwaysShowValidationErrorProperty
= DependencyProperty.RegisterAttached(
"AlwaysShowValidationError",
typeof(bool),
typeof(ValidationHelper),
new PropertyMetadata(BooleanBoxes.FalseBox));

/// <summary>
/// Gets whether the validation error text should always be shown, regardless of focus or mouse position.
/// </summary>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(UIElement))]
public static bool GetAlwaysShowValidationError(UIElement element)
{
return (bool)element.GetValue(AlwaysShowValidationErrorProperty);
}

/// <summary>
/// Sets whether the validation error text should always be shown, regardless of focus or mouse position.
/// </summary>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(UIElement))]
public static void SetAlwaysShowValidationError(UIElement element, bool value)
{
element.SetValue(AlwaysShowValidationErrorProperty, BooleanBoxes.Box(value));
}
}
}
11 changes: 10 additions & 1 deletion src/MahApps.Metro/Styles/Controls.ValidationError.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,23 @@
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=ValidationPopup, Path=CanShow, Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.HasError), Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(mah:ValidationHelper.ShowValidationErrorOnKeyboardFocus), Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=ValidationPopup, Path=ShowValidationErrorOnKeyboardFocus, Mode=OneWay}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="ValidationPopup" Property="IsOpen" Value="True" />
</MultiDataTrigger>

<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=ValidationPopup, Path=CanShow, Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.HasError), Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(mah:ValidationHelper.AlwaysShowValidationError), Mode=OneWay}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="ValidationPopup" Property="IsOpen" Value="True" />
</MultiDataTrigger>

<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=RedTriangle, Path=IsMouseOver, Mode=OneWay}" Value="True" />
Expand Down

0 comments on commit 0fd615b

Please sign in to comment.