diff --git a/src/Avalonia.Base/AvaloniaObject.cs b/src/Avalonia.Base/AvaloniaObject.cs
index 0ce468354c..bcce2080a6 100644
--- a/src/Avalonia.Base/AvaloniaObject.cs
+++ b/src/Avalonia.Base/AvaloniaObject.cs
@@ -9,7 +9,6 @@ using Avalonia.Diagnostics;
using Avalonia.Logging;
using Avalonia.PropertyStore;
using Avalonia.Threading;
-using Avalonia.Utilities;
namespace Avalonia
{
@@ -136,7 +135,8 @@ namespace Avalonia
/// The property.
public void ClearValue(AvaloniaProperty property)
{
- Contract.Requires(property != null);
+ property = property ?? throw new ArgumentNullException(nameof(property));
+
property.RouteClearValue(this);
}
@@ -146,24 +146,47 @@ namespace Avalonia
/// The property.
public void ClearValue(AvaloniaProperty property)
{
+ property = property ?? throw new ArgumentNullException(nameof(property));
VerifyAccess();
switch (property)
{
case StyledPropertyBase styled:
- _values.ClearLocalValue(styled);
+ ClearValue(styled);
break;
case DirectPropertyBase direct:
- var p = AvaloniaPropertyRegistry.Instance.GetRegisteredDirect(this, direct);
- p.InvokeSetter(this, p.GetUnsetValue(GetType()));
+ ClearValue(direct);
break;
- case null:
- throw new ArgumentNullException(nameof(property));
default:
throw new NotSupportedException("Unsupported AvaloniaProperty type.");
}
}
+ ///
+ /// Clears a 's local value.
+ ///
+ /// The property.
+ public void ClearValue(StyledPropertyBase property)
+ {
+ property = property ?? throw new ArgumentNullException(nameof(property));
+ VerifyAccess();
+
+ _values?.ClearLocalValue(property);
+ }
+
+ ///
+ /// Clears a 's local value.
+ ///
+ /// The property.
+ public void ClearValue(DirectPropertyBase property)
+ {
+ property = property ?? throw new ArgumentNullException(nameof(property));
+ VerifyAccess();
+
+ var p = AvaloniaPropertyRegistry.Instance.GetRegisteredDirect(this, property);
+ p.InvokeSetter(this, p.GetUnsetValue(GetType()));
+ }
+
///
/// Compares two objects using reference equality.
///
@@ -202,6 +225,8 @@ namespace Avalonia
/// The value.
public object GetValue(AvaloniaProperty property)
{
+ property = property ?? throw new ArgumentNullException(nameof(property));
+
return property.RouteGetValue(this);
}
@@ -213,11 +238,12 @@ namespace Avalonia
/// The value.
public T GetValue(AvaloniaProperty property)
{
+ property = property ?? throw new ArgumentNullException(nameof(property));
+
return property switch
{
StyledPropertyBase styled => GetValue(styled),
DirectPropertyBase direct => GetValue(direct),
- null => throw new ArgumentNullException(nameof(property)),
_ => throw new NotSupportedException("Unsupported AvaloniaProperty type.")
};
}
@@ -292,6 +318,8 @@ namespace Avalonia
object value,
BindingPriority priority = BindingPriority.LocalValue)
{
+ property = property ?? throw new ArgumentNullException(nameof(property));
+
property.RouteSetValue(this, value, priority);
}
@@ -307,6 +335,8 @@ namespace Avalonia
T value,
BindingPriority priority = BindingPriority.LocalValue)
{
+ property = property ?? throw new ArgumentNullException(nameof(property));
+
switch (property)
{
case StyledPropertyBase styled:
@@ -315,8 +345,6 @@ namespace Avalonia
case DirectPropertyBase direct:
SetValue(direct, value);
break;
- case null:
- throw new ArgumentNullException(nameof(property));
default:
throw new NotSupportedException("Unsupported AvaloniaProperty type.");
}
@@ -386,6 +414,9 @@ namespace Avalonia
IObservable> source,
BindingPriority priority = BindingPriority.LocalValue)
{
+ property = property ?? throw new ArgumentNullException(nameof(property));
+ source = source ?? throw new ArgumentNullException(nameof(source));
+
return property.RouteBind(this, source, priority);
}
@@ -404,11 +435,13 @@ namespace Avalonia
IObservable> source,
BindingPriority priority = BindingPriority.LocalValue)
{
+ property = property ?? throw new ArgumentNullException(nameof(property));
+ source = source ?? throw new ArgumentNullException(nameof(source));
+
return property switch
{
StyledPropertyBase styled => Bind(styled, source, priority),
DirectPropertyBase direct => Bind(direct, source),
- null => throw new ArgumentNullException(nameof(property)),
_ => throw new NotSupportedException("Unsupported AvaloniaProperty type."),
};
}
@@ -429,6 +462,7 @@ namespace Avalonia
BindingPriority priority = BindingPriority.LocalValue)
{
property = property ?? throw new ArgumentNullException(nameof(property));
+ source = source ?? throw new ArgumentNullException(nameof(source));
VerifyAccess();
return Values.AddBinding(property, source, priority);
diff --git a/src/Avalonia.Base/AvaloniaObjectExtensions.cs b/src/Avalonia.Base/AvaloniaObjectExtensions.cs
index 6a513231d5..a4c7fa95a5 100644
--- a/src/Avalonia.Base/AvaloniaObjectExtensions.cs
+++ b/src/Avalonia.Base/AvaloniaObjectExtensions.cs
@@ -125,7 +125,7 @@ namespace Avalonia
/// for the specified property.
///
public static IObservable GetPropertyChangedObservable(
- this IAvaloniaObject o,
+ this IAvaloniaObject o,
AvaloniaProperty property)
{
Contract.Requires(o != null);
@@ -236,6 +236,58 @@ namespace Avalonia
o.GetBindingObservable(property));
}
+ ///
+ /// Binds a to an observable.
+ ///
+ /// The object.
+ /// The property.
+ /// The observable.
+ /// The priority of the binding.
+ ///
+ /// A disposable which can be used to terminate the binding.
+ ///
+ public static IDisposable Bind(
+ this IAvaloniaObject target,
+ AvaloniaProperty property,
+ IObservable> source,
+ BindingPriority priority = BindingPriority.LocalValue)
+ {
+ target = target ?? throw new ArgumentNullException(nameof(target));
+ property = property ?? throw new ArgumentNullException(nameof(property));
+ source = source ?? throw new ArgumentNullException(nameof(source));
+
+ return property.RouteBind(target, source, priority);
+ }
+
+ ///
+ /// Binds a to an observable.
+ ///
+ /// The type of the property.
+ /// The object.
+ /// The property.
+ /// The observable.
+ /// The priority of the binding.
+ ///
+ /// A disposable which can be used to terminate the binding.
+ ///
+ public static IDisposable Bind(
+ this IAvaloniaObject target,
+ AvaloniaProperty property,
+ IObservable> source,
+ BindingPriority priority = BindingPriority.LocalValue)
+ {
+ target = target ?? throw new ArgumentNullException(nameof(target));
+ property = property ?? throw new ArgumentNullException(nameof(property));
+ source = source ?? throw new ArgumentNullException(nameof(source));
+
+ return property switch
+ {
+ StyledPropertyBase styled => target.Bind(styled, source, priority),
+ DirectPropertyBase direct => target.Bind(direct, source),
+ _ => throw new NotSupportedException("Unsupported AvaloniaProperty type."),
+ };
+ }
+
///
/// Binds a to an observable.
///
@@ -252,6 +304,10 @@ namespace Avalonia
IObservable