diff --git a/src/Avalonia.Base/AvaloniaProperty.cs b/src/Avalonia.Base/AvaloniaProperty.cs index 5db4d81f034..96268376cf7 100644 --- a/src/Avalonia.Base/AvaloniaProperty.cs +++ b/src/Avalonia.Base/AvaloniaProperty.cs @@ -225,13 +225,8 @@ protected AvaloniaProperty( /// The default value of the property. /// Whether the property inherits its value. /// The default binding mode for the property. - /// A value validation callback. + /// A value validation callback. /// A value coercion callback. - /// - /// 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. - /// /// A public static StyledProperty Register( string name, @@ -239,8 +234,40 @@ public static StyledProperty Register( bool inherits = false, BindingMode defaultBindingMode = BindingMode.OneWay, Func? validate = null, - Func? coerce = null, - Action? notifying = null) + Func? coerce = null) + where TOwner : AvaloniaObject + { + _ = name ?? throw new ArgumentNullException(nameof(name)); + + var metadata = new StyledPropertyMetadata( + defaultValue, + defaultBindingMode: defaultBindingMode, + coerce: coerce); + + var result = new StyledProperty( + name, + typeof(TOwner), + metadata, + inherits, + validate); + AvaloniaPropertyRegistry.Instance.Register(typeof(TOwner), result); + return result; + } + + /// + /// + /// 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. + /// + internal static StyledProperty Register( + string name, + TValue defaultValue, + bool inherits, + BindingMode defaultBindingMode, + Func? validate, + Func? coerce, + Action? notifying) where TOwner : AvaloniaObject { _ = name ?? throw new ArgumentNullException(nameof(name)); diff --git a/src/Avalonia.Base/StyledElement.cs b/src/Avalonia.Base/StyledElement.cs index 5bf022cd51c..2cdb9731749 100644 --- a/src/Avalonia.Base/StyledElement.cs +++ b/src/Avalonia.Base/StyledElement.cs @@ -41,7 +41,11 @@ public class StyledElement : Animatable, public static readonly StyledProperty DataContextProperty = AvaloniaProperty.Register( nameof(DataContext), + defaultValue: null, inherits: true, + defaultBindingMode: BindingMode.OneWay, + validate: null, + coerce: null, notifying: DataContextNotifying); /// diff --git a/tests/Avalonia.Base.UnitTests/AvaloniaPropertyTests.cs b/tests/Avalonia.Base.UnitTests/AvaloniaPropertyTests.cs index 5733159a235..811d6ba5767 100644 --- a/tests/Avalonia.Base.UnitTests/AvaloniaPropertyTests.cs +++ b/tests/Avalonia.Base.UnitTests/AvaloniaPropertyTests.cs @@ -188,7 +188,12 @@ internal override EffectiveValue CreateEffectiveValue(AvaloniaObject o) private class Class1 : AvaloniaObject { public static readonly StyledProperty FooProperty = - AvaloniaProperty.Register("Foo", "default", notifying: FooNotifying); + AvaloniaProperty.Register("Foo", "default", + inherits: true, + defaultBindingMode: BindingMode.OneWay, + validate: null, + coerce: null, + notifying: FooNotifying); public int NotifyCount { get; private set; }