Skip to content

Commit 05075e8

Browse files
committed
chore: code cleanup
1 parent dc43a1d commit 05075e8

File tree

1 file changed

+62
-117
lines changed

1 file changed

+62
-117
lines changed

src/Wpf.Ui.Violeta/Controls/Layout/AutoGrid.cs

+62-117
Original file line numberDiff line numberDiff line change
@@ -4,86 +4,49 @@
44

55
namespace Wpf.Ui.Controls;
66

7+
#pragma warning disable CS8602 // Dereference of a possibly null reference.
8+
79
/// <summary>
810
/// Defines a flexible grid area that consists of columns and rows.
911
/// Depending on the orientation, either the rows or the columns are auto-generated,
1012
/// and the children's position is set according to their index.
1113
///
1214
/// Partially based on work at http://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/
1315
/// </summary>
14-
public class AutoGrid : System.Windows.Controls.Grid
16+
public class AutoGrid : Grid
1517
{
16-
#region Public Fields
17-
18-
public static readonly DependencyProperty ChildHorizontalAlignmentProperty = DependencyProperty.Register(
19-
name: "ChildHorizontalAlignment",
20-
propertyType: typeof(HorizontalAlignment?),
21-
ownerType: typeof(AutoGrid),
22-
typeMetadata: new FrameworkPropertyMetadata((HorizontalAlignment?)null, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(OnChildHorizontalAlignmentChanged)));
23-
24-
public static readonly DependencyProperty ChildMarginProperty = DependencyProperty.Register(
25-
name: "ChildMargin",
26-
propertyType: typeof(Thickness?),
27-
ownerType: typeof(AutoGrid),
28-
typeMetadata: new FrameworkPropertyMetadata((Thickness?)null, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(OnChildMarginChanged)));
29-
30-
public static readonly DependencyProperty ChildVerticalAlignmentProperty = DependencyProperty.Register(
31-
name: "ChildVerticalAlignment",
32-
propertyType: typeof(VerticalAlignment?),
33-
ownerType: typeof(AutoGrid),
34-
typeMetadata: new FrameworkPropertyMetadata((VerticalAlignment?)null, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(OnChildVerticalAlignmentChanged)));
35-
36-
public static readonly DependencyProperty ColumnCountProperty = DependencyProperty.RegisterAttached(
37-
name: "ColumnCount",
38-
propertyType: typeof(int),
39-
ownerType: typeof(AutoGrid),
40-
defaultMetadata: new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(ColumnCountChanged)));
41-
42-
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.RegisterAttached(
43-
name: "Columns",
44-
propertyType: typeof(string),
45-
ownerType: typeof(AutoGrid),
46-
defaultMetadata: new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(ColumnsChanged)));
47-
48-
public static readonly DependencyProperty ColumnWidthProperty = DependencyProperty.RegisterAttached(
49-
name: "ColumnWidth",
50-
propertyType: typeof(GridLength),
51-
ownerType: typeof(AutoGrid),
52-
defaultMetadata: new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(FixedColumnWidthChanged)));
53-
54-
public static readonly DependencyProperty IsAutoIndexingProperty = DependencyProperty.Register(
55-
name: "IsAutoIndexing",
56-
propertyType: typeof(bool),
57-
ownerType: typeof(AutoGrid),
58-
typeMetadata: new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsMeasure));
59-
60-
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
61-
name: "Orientation",
62-
propertyType: typeof(Orientation),
63-
ownerType: typeof(AutoGrid),
64-
typeMetadata: new FrameworkPropertyMetadata(Orientation.Horizontal, FrameworkPropertyMetadataOptions.AffectsMeasure));
65-
66-
public static readonly DependencyProperty RowCountProperty = DependencyProperty.RegisterAttached(
67-
name: "RowCount",
68-
propertyType: typeof(int),
69-
ownerType: typeof(AutoGrid),
70-
defaultMetadata: new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(RowCountChanged)));
71-
72-
public static readonly DependencyProperty RowHeightProperty = DependencyProperty.RegisterAttached(
73-
name: "RowHeight",
74-
propertyType: typeof(GridLength),
75-
ownerType: typeof(AutoGrid),
76-
defaultMetadata: new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(FixedRowHeightChanged)));
77-
78-
public static readonly DependencyProperty RowsProperty = DependencyProperty.RegisterAttached(
79-
name: "Rows",
80-
propertyType: typeof(string),
81-
ownerType: typeof(AutoGrid),
82-
defaultMetadata: new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(RowsChanged)));
83-
84-
#endregion Public Fields
85-
86-
#region Public Properties
18+
public static readonly DependencyProperty ChildHorizontalAlignmentProperty =
19+
DependencyProperty.Register(nameof(ChildHorizontalAlignment), typeof(HorizontalAlignment?), typeof(AutoGrid), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure, OnChildHorizontalAlignmentChanged));
20+
21+
public static readonly DependencyProperty ChildMarginProperty =
22+
DependencyProperty.Register(nameof(ChildMargin), typeof(Thickness?), typeof(AutoGrid), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure, OnChildMarginChanged));
23+
24+
public static readonly DependencyProperty ChildVerticalAlignmentProperty =
25+
DependencyProperty.Register(nameof(ChildVerticalAlignment), propertyType: typeof(VerticalAlignment?), ownerType: typeof(AutoGrid), typeMetadata: new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsMeasure, OnChildVerticalAlignmentChanged));
26+
27+
public static readonly DependencyProperty ColumnCountProperty =
28+
DependencyProperty.RegisterAttached(nameof(ColumnCount), typeof(int), typeof(AutoGrid), new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(ColumnCountChanged)));
29+
30+
public static readonly DependencyProperty ColumnsProperty =
31+
DependencyProperty.RegisterAttached(nameof(Columns), typeof(string), typeof(AutoGrid), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(ColumnsChanged)));
32+
33+
public static readonly DependencyProperty ColumnWidthProperty =
34+
DependencyProperty.RegisterAttached(nameof(ColumnWidth), typeof(GridLength), typeof(AutoGrid), new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(FixedColumnWidthChanged)));
35+
36+
public static readonly DependencyProperty IsAutoIndexingProperty =
37+
DependencyProperty.Register(nameof(IsAutoIndexing), typeof(bool), typeof(AutoGrid), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsMeasure));
38+
39+
public static readonly DependencyProperty OrientationProperty =
40+
DependencyProperty.Register(nameof(Orientation), typeof(Orientation), typeof(AutoGrid), new FrameworkPropertyMetadata(Orientation.Horizontal, FrameworkPropertyMetadataOptions.AffectsMeasure));
41+
42+
public static readonly DependencyProperty RowCountProperty =
43+
DependencyProperty.RegisterAttached(nameof(RowCount), typeof(int), typeof(AutoGrid), new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(RowCountChanged)));
44+
45+
public static readonly DependencyProperty RowHeightProperty =
46+
DependencyProperty.RegisterAttached(nameof(RowHeight), typeof(GridLength), typeof(AutoGrid), new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(FixedRowHeightChanged)));
47+
48+
public static readonly DependencyProperty RowsProperty =
49+
DependencyProperty.RegisterAttached(nameof(Rows), typeof(string), typeof(AutoGrid), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(RowsChanged)));
8750

8851
/// <summary>
8952
/// Gets or sets the child horizontal alignment.
@@ -92,8 +55,8 @@ public class AutoGrid : System.Windows.Controls.Grid
9255
[Category("Layout"), Description("Presets the horizontal alignment of all child controls")]
9356
public HorizontalAlignment? ChildHorizontalAlignment
9457
{
95-
get { return (HorizontalAlignment?)GetValue(ChildHorizontalAlignmentProperty); }
96-
set { SetValue(ChildHorizontalAlignmentProperty, value); }
58+
get => (HorizontalAlignment?)GetValue(ChildHorizontalAlignmentProperty);
59+
set => SetValue(ChildHorizontalAlignmentProperty, value);
9760
}
9861

9962
/// <summary>
@@ -103,8 +66,8 @@ public HorizontalAlignment? ChildHorizontalAlignment
10366
[Category("Layout"), Description("Presets the margin of all child controls")]
10467
public Thickness? ChildMargin
10568
{
106-
get { return (Thickness?)GetValue(ChildMarginProperty); }
107-
set { SetValue(ChildMarginProperty, value); }
69+
get => (Thickness?)GetValue(ChildMarginProperty);
70+
set => SetValue(ChildMarginProperty, value);
10871
}
10972

11073
/// <summary>
@@ -114,8 +77,8 @@ public Thickness? ChildMargin
11477
[Category("Layout"), Description("Presets the vertical alignment of all child controls")]
11578
public VerticalAlignment? ChildVerticalAlignment
11679
{
117-
get { return (VerticalAlignment?)GetValue(ChildVerticalAlignmentProperty); }
118-
set { SetValue(ChildVerticalAlignmentProperty, value); }
80+
get => (VerticalAlignment?)GetValue(ChildVerticalAlignmentProperty);
81+
set => SetValue(ChildVerticalAlignmentProperty, value);
11982
}
12083

12184
/// <summary>
@@ -124,8 +87,8 @@ public VerticalAlignment? ChildVerticalAlignment
12487
[Category("Layout"), Description("Defines a set number of columns")]
12588
public int ColumnCount
12689
{
127-
get { return (int)GetValue(ColumnCountProperty); }
128-
set { SetValue(ColumnCountProperty, value); }
90+
get => (int)GetValue(ColumnCountProperty);
91+
set => SetValue(ColumnCountProperty, value);
12992
}
13093

13194
/// <summary>
@@ -134,8 +97,8 @@ public int ColumnCount
13497
[Category("Layout"), Description("Defines all columns using comma separated grid length notation")]
13598
public string Columns
13699
{
137-
get { return (string)GetValue(ColumnsProperty); }
138-
set { SetValue(ColumnsProperty, value); }
100+
get => (string)GetValue(ColumnsProperty);
101+
set => SetValue(ColumnsProperty, value);
139102
}
140103

141104
/// <summary>
@@ -144,8 +107,8 @@ public string Columns
144107
[Category("Layout"), Description("Presets the width of all columns set using the ColumnCount property")]
145108
public GridLength ColumnWidth
146109
{
147-
get { return (GridLength)GetValue(ColumnWidthProperty); }
148-
set { SetValue(ColumnWidthProperty, value); }
110+
get => (GridLength)GetValue(ColumnWidthProperty);
111+
set => SetValue(ColumnWidthProperty, value);
149112
}
150113

151114
/// <summary>
@@ -158,8 +121,8 @@ public GridLength ColumnWidth
158121
[Category("Layout"), Description("Set to false to disable the auto layout functionality")]
159122
public bool IsAutoIndexing
160123
{
161-
get { return (bool)GetValue(IsAutoIndexingProperty); }
162-
set { SetValue(IsAutoIndexingProperty, value); }
124+
get => (bool)GetValue(IsAutoIndexingProperty);
125+
set => SetValue(IsAutoIndexingProperty, value);
163126
}
164127

165128
/// <summary>
@@ -170,8 +133,8 @@ public bool IsAutoIndexing
170133
[Category("Layout"), Description("Defines the directionality of the autolayout. Use vertical for a column first layout, horizontal for a row first layout.")]
171134
public Orientation Orientation
172135
{
173-
get { return (Orientation)GetValue(OrientationProperty); }
174-
set { SetValue(OrientationProperty, value); }
136+
get => (Orientation)GetValue(OrientationProperty);
137+
set => SetValue(OrientationProperty, value);
175138
}
176139

177140
/// <summary>
@@ -180,8 +143,8 @@ public Orientation Orientation
180143
[Category("Layout"), Description("Defines a set number of rows")]
181144
public int RowCount
182145
{
183-
get { return (int)GetValue(RowCountProperty); }
184-
set { SetValue(RowCountProperty, value); }
146+
get => (int)GetValue(RowCountProperty);
147+
set => SetValue(RowCountProperty, value);
185148
}
186149

187150
/// <summary>
@@ -190,8 +153,8 @@ public int RowCount
190153
[Category("Layout"), Description("Presets the height of all rows set using the RowCount property")]
191154
public GridLength RowHeight
192155
{
193-
get { return (GridLength)GetValue(RowHeightProperty); }
194-
set { SetValue(RowHeightProperty, value); }
156+
get => (GridLength)GetValue(RowHeightProperty);
157+
set => SetValue(RowHeightProperty, value);
195158
}
196159

197160
/// <summary>
@@ -200,14 +163,10 @@ public GridLength RowHeight
200163
[Category("Layout"), Description("Defines all rows using comma separated grid length notation")]
201164
public string Rows
202165
{
203-
get { return (string)GetValue(RowsProperty); }
204-
set { SetValue(RowsProperty, value); }
166+
get => (string)GetValue(RowsProperty);
167+
set => SetValue(RowsProperty, value);
205168
}
206169

207-
#endregion Public Properties
208-
209-
#region Public Methods
210-
211170
/// <summary>
212171
/// Handles the column count changed event
213172
/// </summary>
@@ -351,10 +310,6 @@ public static void RowsChanged(DependencyObject d, DependencyPropertyChangedEven
351310
grid.RowDefinitions.Add(new RowDefinition() { Height = def });
352311
}
353312

354-
#endregion Public Methods
355-
356-
#region Protected Methods
357-
358313
/// <summary>
359314
/// Measures the children of a <see cref="T:System.Windows.Controls.Grid"/> in anticipation of arranging them during the <see cref="M:ArrangeOverride"/> pass.
360315
/// </summary>
@@ -368,10 +323,6 @@ protected override Size MeasureOverride(Size constraint)
368323
return base.MeasureOverride(constraint);
369324
}
370325

371-
#endregion Protected Methods
372-
373-
#region Private Methods
374-
375326
/// <summary>
376327
/// Called when [child horizontal alignment changed].
377328
/// </summary>
@@ -455,9 +406,9 @@ private int Clamp(int value, int max)
455406
/// </summary>
456407
private void PerformLayout()
457408
{
458-
var fillRowFirst = this.Orientation == Orientation.Horizontal;
459-
var rowCount = this.RowDefinitions.Count;
460-
var colCount = this.ColumnDefinitions.Count;
409+
var fillRowFirst = Orientation == Orientation.Horizontal;
410+
var rowCount = RowDefinitions.Count;
411+
var colCount = ColumnDefinitions.Count;
461412

462413
if (rowCount == 0 || colCount == 0)
463414
return;
@@ -516,14 +467,10 @@ private void PerformLayout()
516467
ApplyChildLayout(child);
517468
}
518469
}
519-
520-
#endregion Private Methods
521470
}
522471

523-
public static class DependencyExtensions
472+
file static class DependencyExtensions
524473
{
525-
#region Public Methods
526-
527474
/// <summary>
528475
/// Sets the value of the <paramref name="property"/> only if it hasn't been explicitly set.
529476
/// </summary>
@@ -538,6 +485,4 @@ public static bool SetIfDefault<T>(this DependencyObject o, DependencyProperty p
538485

539486
return false;
540487
}
541-
542-
#endregion Public Methods
543-
}
488+
}

0 commit comments

Comments
 (0)