From 83827a93f594ce2a2ffc82847bc8d0f8b0f2c326 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Thu, 30 May 2019 15:39:14 +0800 Subject: [PATCH] Convert DependencyProperties to Avalonia's. --- src/Avalonia.Controls/Grid.cs | 161 ++++++++++------------------------ 1 file changed, 47 insertions(+), 114 deletions(-) diff --git a/src/Avalonia.Controls/Grid.cs b/src/Avalonia.Controls/Grid.cs index 6f4d7bc063..4e77d8b307 100644 --- a/src/Avalonia.Controls/Grid.cs +++ b/src/Avalonia.Controls/Grid.cs @@ -28,6 +28,8 @@ namespace Avalonia.Controls static Grid() { + ShowGridLinesProperty.Changed.AddClassHandler(OnShowGridLinesPropertyChanged); + AffectsParentMeasure(ColumnProperty, ColumnSpanProperty, RowProperty, RowSpanProperty); } /// @@ -35,7 +37,6 @@ namespace Avalonia.Controls /// public Grid() { - SetFlags((bool) ShowGridLinesProperty.GetDefaultValue(AvaloniaObjectType), Flags.ShowGridLinesPropertyValue); } //------------------------------------------------------ @@ -81,11 +82,7 @@ namespace Avalonia.Controls /// Column property value. public static void SetColumn(Control element, int value) { - if (element == null) - { - throw new ArgumentNullException("element"); - } - + Contract.Requires(element != null); element.SetValue(ColumnProperty, value); } @@ -96,12 +93,8 @@ namespace Avalonia.Controls /// Column property value. public static int GetColumn(Control element) { - if (element == null) - { - throw new ArgumentNullException("element"); - } - - return ((int)element.GetValue(ColumnProperty)); + Contract.Requires(element != null); + return element.GetValue(ColumnProperty); } /// @@ -111,11 +104,7 @@ namespace Avalonia.Controls /// Row property value. public static void SetRow(Control element, int value) { - if (element == null) - { - throw new ArgumentNullException("element"); - } - + Contract.Requires(element != null); element.SetValue(RowProperty, value); } @@ -126,12 +115,8 @@ namespace Avalonia.Controls /// Row property value. public static int GetRow(Control element) { - if (element == null) - { - throw new ArgumentNullException("element"); - } - - return ((int)element.GetValue(RowProperty)); + Contract.Requires(element != null); + return element.GetValue(RowProperty); } /// @@ -141,11 +126,7 @@ namespace Avalonia.Controls /// ColumnSpan property value. public static void SetColumnSpan(Control element, int value) { - if (element == null) - { - throw new ArgumentNullException("element"); - } - + Contract.Requires(element != null); element.SetValue(ColumnSpanProperty, value); } @@ -156,12 +137,8 @@ namespace Avalonia.Controls /// ColumnSpan property value. public static int GetColumnSpan(Control element) { - if (element == null) - { - throw new ArgumentNullException("element"); - } - - return ((int)element.GetValue(ColumnSpanProperty)); + Contract.Requires(element != null); + return element.GetValue(ColumnSpanProperty); } /// @@ -171,11 +148,7 @@ namespace Avalonia.Controls /// RowSpan property value. public static void SetRowSpan(Control element, int value) { - if (element == null) - { - throw new ArgumentNullException("element"); - } - + Contract.Requires(element != null); element.SetValue(RowSpanProperty, value); } @@ -186,12 +159,8 @@ namespace Avalonia.Controls /// RowSpan property value. public static int GetRowSpan(Control element) { - if (element == null) - { - throw new ArgumentNullException("element"); - } - - return ((int)element.GetValue(RowSpanProperty)); + Contract.Requires(element != null); + return element.GetValue(RowSpanProperty); } /// @@ -201,11 +170,7 @@ namespace Avalonia.Controls /// IsSharedSizeScope property value. public static void SetIsSharedSizeScope(Control element, bool value) { - if (element == null) - { - throw new ArgumentNullException("element"); - } - + Contract.Requires(element != null); element.SetValue(IsSharedSizeScopeProperty, value); } @@ -216,12 +181,8 @@ namespace Avalonia.Controls /// IsSharedSizeScope property value. public static bool GetIsSharedSizeScope(Control element) { - if (element == null) - { - throw new ArgumentNullException("element"); - } - - return ((bool)element.GetValue(IsSharedSizeScopeProperty)); + Contract.Requires(element != null); + return element.GetValue(IsSharedSizeScopeProperty); } //------------------------------------------------------ @@ -235,7 +196,7 @@ namespace Avalonia.Controls /// public bool ShowGridLines { - get { return (CheckFlagsAnd(Flags.ShowGridLinesPropertyValue)); } + get { return GetValue(ShowGridLinesProperty); } set { SetValue(ShowGridLinesProperty, value); } } @@ -3304,14 +3265,8 @@ namespace Avalonia.Controls /// to true grid lines are drawn to visualize location /// of grid lines. /// - public static readonly AvaloniaProperty ShowGridLinesProperty = - AvaloniaProperty.Register( - "ShowGridLines", - typeof(bool), - typeof(Grid), - new FrameworkPropertyMetadata( - false, - new PropertyChangedCallback(OnShowGridLinesPropertyChanged))); + public static readonly StyledProperty ShowGridLinesProperty = + AvaloniaProperty.Register(nameof(ShowGridLines)); /// /// Column property. This is an attached property. @@ -3324,16 +3279,12 @@ namespace Avalonia.Controls /// should have Column property set to 0. /// Default value for the property is 0. /// - [CommonAvaloniaProperty] - public static readonly AvaloniaProperty ColumnProperty = - AvaloniaProperty.RegisterAttached( - "Column", - typeof(int), - typeof(Grid), - new FrameworkPropertyMetadata( - 0, - new PropertyChangedCallback(OnCellAttachedPropertyChanged)), - new ValidateValueCallback(IsIntValueNotNegative)); + public static readonly AttachedProperty ColumnProperty = + AvaloniaProperty.RegisterAttached( + "Column", + defaultValue: 0, + validate: (_, v) => { if (v >= 0) return v; + else throw new ArgumentException("Invalid Grid.Column value."); }); /// /// Row property. This is an attached property. @@ -3346,16 +3297,12 @@ namespace Avalonia.Controls /// Default value for the property is 0. /// /// - [CommonAvaloniaProperty] - public static readonly AvaloniaProperty RowProperty = - AvaloniaProperty.RegisterAttached( - "Row", - typeof(int), - typeof(Grid), - new FrameworkPropertyMetadata( - 0, - new PropertyChangedCallback(OnCellAttachedPropertyChanged)), - new ValidateValueCallback(IsIntValueNotNegative)); + public static readonly AttachedProperty RowProperty = + AvaloniaProperty.RegisterAttached( + "Row", + defaultValue: 0, + validate: (_, v) => { if (v >= 0) return v; + else throw new ArgumentException("Invalid Grid.Row value."); }); /// /// ColumnSpan property. This is an attached property. @@ -3367,16 +3314,12 @@ namespace Avalonia.Controls /// /// Default value for the property is 1. /// - [CommonAvaloniaProperty] - public static readonly AvaloniaProperty ColumnSpanProperty = - AvaloniaProperty.RegisterAttached( - "ColumnSpan", - typeof(int), - typeof(Grid), - new FrameworkPropertyMetadata( - 1, - new PropertyChangedCallback(OnCellAttachedPropertyChanged)), - new ValidateValueCallback(IsIntValueGreaterThanZero)); + public static readonly AttachedProperty ColumnSpanProperty = + AvaloniaProperty.RegisterAttached( + "ColumnSpan", + defaultValue: 1, + validate: (_, v) => { if (v >= 1) return v; + else throw new ArgumentException("Invalid Grid.ColumnSpan value."); }); /// /// RowSpan property. This is an attached property. @@ -3388,29 +3331,19 @@ namespace Avalonia.Controls /// /// Default value for the property is 1. /// - [CommonAvaloniaProperty] - public static readonly AvaloniaProperty RowSpanProperty = - AvaloniaProperty.RegisterAttached( - "RowSpan", - typeof(int), - typeof(Grid), - new FrameworkPropertyMetadata( - 1, - new PropertyChangedCallback(OnCellAttachedPropertyChanged)), - new ValidateValueCallback(IsIntValueGreaterThanZero)); - + public static readonly AttachedProperty RowSpanProperty = + AvaloniaProperty.RegisterAttached( + "RowSpan", + defaultValue: 1, + validate: (_, v) => { if (v >= 1) return v; + else throw new ArgumentException("Invalid Grid.RowSpan value."); }); /// /// IsSharedSizeScope property marks scoping element for shared size. /// - public static readonly AvaloniaProperty IsSharedSizeScopeProperty = - AvaloniaProperty.RegisterAttached( - "IsSharedSizeScope", - typeof(bool), - typeof(Grid), - new FrameworkPropertyMetadata( - false, - new PropertyChangedCallback(DefinitionBase.OnIsSharedSizeScopePropertyChanged))); + public static readonly AttachedProperty IsSharedSizeScopeProperty = + AvaloniaProperty.RegisterAttached( + "IsSharedSizeScope"); //------------------------------------------------------ //