Browse Source

Make AvaloniaProperty.Register overload with "notifying" parameter internal

Max Katz 2 years ago
parent
commit
4fc1995246

+ 35 - 8
src/Avalonia.Base/AvaloniaProperty.cs

@@ -225,13 +225,8 @@ namespace Avalonia
         /// <param name="defaultValue">The default value of the property.</param>
         /// <param name="inherits">Whether the property inherits its value.</param>
         /// <param name="defaultBindingMode">The default binding mode for the property.</param>
-         /// <param name="validate">A value validation callback.</param>
+        /// <param name="validate">A value validation callback.</param>
         /// <param name="coerce">A value coercion callback.</param>
-        /// <param name="notifying">
-        /// A method that gets called before and after the property starts being notified on an
-        /// object; the bool argument will be true before and false afterwards. This callback is
-        /// intended to support IsDataContextChanging.
-        /// </param>
         /// <returns>A <see cref="StyledProperty{TValue}"/></returns>
         public static StyledProperty<TValue> Register<TOwner, TValue>(
             string name,
@@ -239,8 +234,40 @@ namespace Avalonia
             bool inherits = false,
             BindingMode defaultBindingMode = BindingMode.OneWay,
             Func<TValue, bool>? validate = null,
-            Func<AvaloniaObject, TValue, TValue>? coerce = null,
-            Action<AvaloniaObject, bool>? notifying = null)
+            Func<AvaloniaObject, TValue, TValue>? coerce = null)
+                where TOwner : AvaloniaObject
+        {
+            _ = name ?? throw new ArgumentNullException(nameof(name));
+
+            var metadata = new StyledPropertyMetadata<TValue>(
+                defaultValue,
+                defaultBindingMode: defaultBindingMode,
+                coerce: coerce);
+
+            var result = new StyledProperty<TValue>(
+                name,
+                typeof(TOwner),
+                metadata,
+                inherits,
+                validate);
+            AvaloniaPropertyRegistry.Instance.Register(typeof(TOwner), result);
+            return result;
+        }
+        
+        /// <inheritdoc cref="Register{TOwner, TValue}" />
+        /// <param name="notifying">
+        /// A method that gets called before and after the property starts being notified on an
+        /// object; the bool argument will be true before and false afterwards. This callback is
+        /// intended to support IsDataContextChanging.
+        /// </param>
+        internal static StyledProperty<TValue> Register<TOwner, TValue>(
+            string name,
+            TValue defaultValue,
+            bool inherits,
+            BindingMode defaultBindingMode,
+            Func<TValue, bool>? validate,
+            Func<AvaloniaObject, TValue, TValue>? coerce,
+            Action<AvaloniaObject, bool>? notifying)
                 where TOwner : AvaloniaObject
         {
             _ = name ?? throw new ArgumentNullException(nameof(name));

+ 4 - 0
src/Avalonia.Base/StyledElement.cs

@@ -41,7 +41,11 @@ namespace Avalonia
         public static readonly StyledProperty<object?> DataContextProperty =
             AvaloniaProperty.Register<StyledElement, object?>(
                 nameof(DataContext),
+                defaultValue: null,
                 inherits: true,
+                defaultBindingMode: BindingMode.OneWay,
+                validate: null,
+                coerce: null,
                 notifying: DataContextNotifying);
 
         /// <summary>

+ 6 - 1
tests/Avalonia.Base.UnitTests/AvaloniaPropertyTests.cs

@@ -188,7 +188,12 @@ namespace Avalonia.Base.UnitTests
         private class Class1 : AvaloniaObject
         {
             public static readonly StyledProperty<string> FooProperty =
-                AvaloniaProperty.Register<Class1, string>("Foo", "default", notifying: FooNotifying);
+                AvaloniaProperty.Register<Class1, string>("Foo", "default",
+                    inherits: true,
+                    defaultBindingMode: BindingMode.OneWay,
+                    validate: null,
+                    coerce: null,
+                    notifying: FooNotifying);
 
             public int NotifyCount { get; private set; }