Browse Source

Fix ComboBox not showing Text property validation (#20978)

release/11.3.13
Petar Tasev 6 months ago
committed by Julien Lebosquain
parent
commit
4d0ff8c26d
No known key found for this signature in database GPG Key ID: 1833CAD10ACC46FD
  1. 11
      src/Avalonia.Controls/ComboBox.cs
  2. 22
      tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

11
src/Avalonia.Controls/ComboBox.cs

@ -85,7 +85,8 @@ namespace Avalonia.Controls
/// Defines the <see cref="Text"/> property
/// </summary>
public static readonly StyledProperty<string?> TextProperty =
TextBlock.TextProperty.AddOwner<ComboBox>(new(string.Empty, BindingMode.TwoWay));
TextBlock.TextProperty.AddOwner<ComboBox>(new(string.Empty, BindingMode.TwoWay,
enableDataValidation: true));
/// <summary>
/// Defines the <see cref="SelectionBoxItemTemplate"/> property.
@ -236,6 +237,14 @@ namespace Avalonia.Controls
UpdateFlowDirection();
}
protected override void UpdateDataValidation(AvaloniaProperty property, BindingValueType state, Exception? error)
{
base.UpdateDataValidation(property, state, error);
if (property == TextProperty)
DataValidationErrors.SetError(this, error);
}
protected internal override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
{
return new ComboBoxItem();

22
tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

@ -391,6 +391,28 @@ namespace Avalonia.Controls.UnitTests
}
[Fact]
public void Text_Validation()
{
using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
{
var target = new ComboBox
{
Template = GetTemplate(),
};
target.ApplyTemplate();
target.Presenter!.ApplyTemplate();
var exception = new System.InvalidCastException("failed validation");
var textObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
target.Bind(ComboBox.TextProperty, textObservable);
Assert.True(DataValidationErrors.GetHasErrors(target));
Assert.Equal([exception], DataValidationErrors.GetErrors(target));
}
}
[Fact]
public void Close_Window_On_Alt_F4_When_ComboBox_Is_Focus()
{

Loading…
Cancel
Save