Skip to content

Commit 138d087

Browse files
authored
Fixing issue with validation template not updating when current error changes (#3182)
Fixes #3176
1 parent 16387a2 commit 138d087

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<UserControl x:Class="MaterialDesignThemes.UITests.Samples.Validation.ValidationUpdates"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
mc:Ignorable="d"
7+
d:DesignHeight="450" d:DesignWidth="800">
8+
<Grid>
9+
<Grid.RowDefinitions>
10+
<RowDefinition />
11+
<RowDefinition />
12+
</Grid.RowDefinitions>
13+
14+
<TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"
15+
VerticalAlignment="Center"/>
16+
17+
<Button Command="{Binding CauseErrorsCommand}" Content="Validate" Grid.Row="1" VerticalAlignment="Center" />
18+
</Grid>
19+
</UserControl>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections;
2+
using System.ComponentModel;
3+
using System.ComponentModel.DataAnnotations;
4+
using CommunityToolkit.Mvvm.ComponentModel;
5+
using CommunityToolkit.Mvvm.Input;
6+
7+
namespace MaterialDesignThemes.UITests.Samples.Validation;
8+
9+
/// <summary>
10+
/// Interaction logic for ValidationUpdates.xaml
11+
/// </summary>
12+
public partial class ValidationUpdates
13+
{
14+
public ValidationUpdates()
15+
{
16+
DataContext = new ValidationUpdatesViewModel();
17+
InitializeComponent();
18+
}
19+
}
20+
21+
public partial class ValidationUpdatesViewModel : ObservableObject, INotifyDataErrorInfo
22+
{
23+
[ObservableProperty]
24+
private string? _text;
25+
26+
private string? Error { get; set; }
27+
28+
public bool HasErrors => Error != null;
29+
30+
public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;
31+
32+
public IEnumerable GetErrors(string? propertyName)
33+
{
34+
if (propertyName == nameof(Text))
35+
{
36+
if (Error != null)
37+
{
38+
return new[] { Error };
39+
}
40+
}
41+
return Enumerable.Empty<string>();
42+
}
43+
44+
[RelayCommand]
45+
private async Task CauseErrors()
46+
{
47+
Error = "Some error";
48+
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Text)));
49+
await Task.Delay(100);
50+
Error += " + more";
51+
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Text)));
52+
}
53+
}

MaterialDesignThemes.UITests/WPF/TextBoxes/TextBoxTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.ComponentModel;
22
using System.Globalization;
33
using System.Windows.Media;
4+
using MaterialDesignThemes.UITests.Samples.Validation;
45

56
namespace MaterialDesignThemes.UITests.WPF.TextBoxes;
67

@@ -590,6 +591,26 @@ public async Task TextBox_MultiLineAndFixedHeight_RespectsVerticalContentAlignme
590591

591592
recorder.Success();
592593
}
594+
595+
[Fact]
596+
[Description("Issue 3176")]
597+
public async Task ValidationErrorTemplate_WithChangingErrors_UpdatesValidation()
598+
{
599+
await using var recorder = new TestRecorder(App);
600+
601+
IVisualElement userControl = await LoadUserControl<ValidationUpdates>();
602+
var textBox = await userControl.GetElement<TextBox>();
603+
var button = await userControl.GetElement<Button>();
604+
await button.LeftClick();
605+
606+
await Wait.For(async() =>
607+
{
608+
var errorViewer = await textBox.GetElement("DefaultErrorViewer");
609+
var textBlock = await errorViewer.GetElement<TextBlock>();
610+
611+
Assert.Equal("Some error + more", await textBlock.GetText());
612+
});
613+
}
593614
}
594615

595616
public class NotEmptyValidationRule : ValidationRule

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ValidationErrorTemplate.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
HorizontalAlignment="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.HorizontalAlignment)}"
3333
FontSize="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.FontSize)}"
3434
Foreground="{DynamicResource MaterialDesignValidationErrorBrush}"
35-
Text="{Binding CurrentItem.ErrorContent, Mode=OneTime}"
35+
Text="{Binding /ErrorContent, Mode=OneTime}"
3636
TextWrapping="Wrap"
3737
UseLayoutRounding="false" />
3838
</Border>
@@ -46,7 +46,7 @@
4646
HorizontalAlignment="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.HorizontalAlignment)}"
4747
FontSize="{Binding ElementName=Placeholder, Path=AdornedElement.(wpf:ValidationAssist.FontSize)}"
4848
Foreground="{DynamicResource MaterialDesignValidationErrorBrush}"
49-
Text="{Binding CurrentItem.ErrorContent, Mode=OneTime}"
49+
Text="{Binding /ErrorContent, Mode=OneTime}"
5050
TextWrapping="Wrap"
5151
UseLayoutRounding="false" />
5252
</Border>

0 commit comments

Comments
 (0)