Skip to content

Commit 4d0ff8c

Browse files
ptasevMrJul
authored andcommitted
Fix ComboBox not showing Text property validation (#20978)
1 parent 116a202 commit 4d0ff8c

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/Avalonia.Controls/ComboBox.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public class ComboBox : SelectingItemsControl
8585
/// Defines the <see cref="Text"/> property
8686
/// </summary>
8787
public static readonly StyledProperty<string?> TextProperty =
88-
TextBlock.TextProperty.AddOwner<ComboBox>(new(string.Empty, BindingMode.TwoWay));
88+
TextBlock.TextProperty.AddOwner<ComboBox>(new(string.Empty, BindingMode.TwoWay,
89+
enableDataValidation: true));
8990

9091
/// <summary>
9192
/// Defines the <see cref="SelectionBoxItemTemplate"/> property.
@@ -236,6 +237,14 @@ protected internal override void InvalidateMirrorTransform()
236237
UpdateFlowDirection();
237238
}
238239

240+
protected override void UpdateDataValidation(AvaloniaProperty property, BindingValueType state, Exception? error)
241+
{
242+
base.UpdateDataValidation(property, state, error);
243+
244+
if (property == TextProperty)
245+
DataValidationErrors.SetError(this, error);
246+
}
247+
239248
protected internal override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
240249
{
241250
return new ComboBoxItem();

tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,28 @@ public void SelectedItem_Validation()
391391

392392
}
393393

394+
[Fact]
395+
public void Text_Validation()
396+
{
397+
using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
398+
{
399+
var target = new ComboBox
400+
{
401+
Template = GetTemplate(),
402+
};
403+
404+
target.ApplyTemplate();
405+
target.Presenter!.ApplyTemplate();
406+
407+
var exception = new System.InvalidCastException("failed validation");
408+
var textObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
409+
target.Bind(ComboBox.TextProperty, textObservable);
410+
411+
Assert.True(DataValidationErrors.GetHasErrors(target));
412+
Assert.Equal([exception], DataValidationErrors.GetErrors(target));
413+
}
414+
}
415+
394416
[Fact]
395417
public void Close_Window_On_Alt_F4_When_ComboBox_Is_Focus()
396418
{

0 commit comments

Comments
 (0)