|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Reactive.Subjects; |
|
|
|
using Avalonia.Data; |
|
|
|
using Avalonia.UnitTests; |
|
|
|
using Xunit; |
|
|
|
|
|
|
|
namespace Avalonia.Base.UnitTests |
|
|
|
@ -34,10 +35,10 @@ namespace Avalonia.Base.UnitTests |
|
|
|
{ |
|
|
|
var target = new Class1(); |
|
|
|
|
|
|
|
target.SetValue(Class1.ValidatedDirectProperty, new BindingNotification(6)); |
|
|
|
target.SetValue(Class1.ValidatedDirectProperty, new BindingNotification(new Exception(), BindingErrorType.Error)); |
|
|
|
target.SetValue(Class1.ValidatedDirectProperty, new BindingNotification(new Exception(), BindingErrorType.DataValidationError)); |
|
|
|
target.SetValue(Class1.ValidatedDirectProperty, new BindingNotification(7)); |
|
|
|
target.SetValue(Class1.ValidatedDirectIntProperty, new BindingNotification(6)); |
|
|
|
target.SetValue(Class1.ValidatedDirectIntProperty, new BindingNotification(new Exception(), BindingErrorType.Error)); |
|
|
|
target.SetValue(Class1.ValidatedDirectIntProperty, new BindingNotification(new Exception(), BindingErrorType.DataValidationError)); |
|
|
|
target.SetValue(Class1.ValidatedDirectIntProperty, new BindingNotification(7)); |
|
|
|
|
|
|
|
Assert.Equal( |
|
|
|
new[] |
|
|
|
@ -73,7 +74,7 @@ namespace Avalonia.Base.UnitTests |
|
|
|
var source = new Subject<object>(); |
|
|
|
var target = new Class1 |
|
|
|
{ |
|
|
|
[!Class1.ValidatedDirectProperty] = source.ToBinding(), |
|
|
|
[!Class1.ValidatedDirectIntProperty] = source.ToBinding(), |
|
|
|
}; |
|
|
|
|
|
|
|
source.OnNext(new BindingNotification(6)); |
|
|
|
@ -92,6 +93,30 @@ namespace Avalonia.Base.UnitTests |
|
|
|
target.Notifications.AsEnumerable()); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Bound_Validated_Direct_String_Property_Can_Be_Set_To_Null() |
|
|
|
{ |
|
|
|
var source = new ViewModel |
|
|
|
{ |
|
|
|
StringValue = "foo", |
|
|
|
}; |
|
|
|
|
|
|
|
var target = new Class1 |
|
|
|
{ |
|
|
|
[!Class1.ValidatedDirectStringProperty] = new Binding |
|
|
|
{ |
|
|
|
Path = nameof(ViewModel.StringValue), |
|
|
|
Source = source, |
|
|
|
}, |
|
|
|
}; |
|
|
|
|
|
|
|
Assert.Equal("foo", target.ValidatedDirectString); |
|
|
|
|
|
|
|
source.StringValue = null; |
|
|
|
|
|
|
|
Assert.Null(target.ValidatedDirectString); |
|
|
|
} |
|
|
|
|
|
|
|
private class Class1 : AvaloniaObject |
|
|
|
{ |
|
|
|
public static readonly StyledProperty<int> NonValidatedProperty = |
|
|
|
@ -104,15 +129,23 @@ namespace Avalonia.Base.UnitTests |
|
|
|
o => o.NonValidatedDirect, |
|
|
|
(o, v) => o.NonValidatedDirect = v); |
|
|
|
|
|
|
|
public static readonly DirectProperty<Class1, int> ValidatedDirectProperty = |
|
|
|
public static readonly DirectProperty<Class1, int> ValidatedDirectIntProperty = |
|
|
|
AvaloniaProperty.RegisterDirect<Class1, int>( |
|
|
|
nameof(ValidatedDirect), |
|
|
|
o => o.ValidatedDirect, |
|
|
|
(o, v) => o.ValidatedDirect = v, |
|
|
|
nameof(ValidatedDirectInt), |
|
|
|
o => o.ValidatedDirectInt, |
|
|
|
(o, v) => o.ValidatedDirectInt = v, |
|
|
|
enableDataValidation: true); |
|
|
|
|
|
|
|
public static readonly DirectProperty<Class1, string> ValidatedDirectStringProperty = |
|
|
|
AvaloniaProperty.RegisterDirect<Class1, string>( |
|
|
|
nameof(ValidatedDirectString), |
|
|
|
o => o.ValidatedDirectString, |
|
|
|
(o, v) => o.ValidatedDirectString = v, |
|
|
|
enableDataValidation: true); |
|
|
|
|
|
|
|
private int _nonValidatedDirect; |
|
|
|
private int _direct; |
|
|
|
private int _directInt; |
|
|
|
private string _directString; |
|
|
|
|
|
|
|
public int NonValidated |
|
|
|
{ |
|
|
|
@ -122,14 +155,20 @@ namespace Avalonia.Base.UnitTests |
|
|
|
|
|
|
|
public int NonValidatedDirect |
|
|
|
{ |
|
|
|
get { return _direct; } |
|
|
|
get { return _directInt; } |
|
|
|
set { SetAndRaise(NonValidatedDirectProperty, ref _nonValidatedDirect, value); } |
|
|
|
} |
|
|
|
|
|
|
|
public int ValidatedDirect |
|
|
|
public int ValidatedDirectInt |
|
|
|
{ |
|
|
|
get { return _directInt; } |
|
|
|
set { SetAndRaise(ValidatedDirectIntProperty, ref _directInt, value); } |
|
|
|
} |
|
|
|
|
|
|
|
public string ValidatedDirectString |
|
|
|
{ |
|
|
|
get { return _direct; } |
|
|
|
set { SetAndRaise(ValidatedDirectProperty, ref _direct, value); } |
|
|
|
get { return _directString; } |
|
|
|
set { SetAndRaise(ValidatedDirectStringProperty, ref _directString, value); } |
|
|
|
} |
|
|
|
|
|
|
|
public IList<BindingNotification> Notifications { get; } = new List<BindingNotification>(); |
|
|
|
@ -139,5 +178,16 @@ namespace Avalonia.Base.UnitTests |
|
|
|
Notifications.Add(notification); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public class ViewModel : NotifyingBase |
|
|
|
{ |
|
|
|
private string _stringValue; |
|
|
|
|
|
|
|
public string StringValue |
|
|
|
{ |
|
|
|
get { return _stringValue; } |
|
|
|
set { _stringValue = value; RaisePropertyChanged(); } |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|