4
4
5
5
namespace Wpf . Ui . Controls ;
6
6
7
+ #pragma warning disable CS8602 // Dereference of a possibly null reference.
8
+
7
9
/// <summary>
8
10
/// Defines a flexible grid area that consists of columns and rows.
9
11
/// Depending on the orientation, either the rows or the columns are auto-generated,
10
12
/// and the children's position is set according to their index.
11
13
///
12
14
/// Partially based on work at http://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/
13
15
/// </summary>
14
- public class AutoGrid : System . Windows . Controls . Grid
16
+ public class AutoGrid : Grid
15
17
{
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 ) ) ) ;
87
50
88
51
/// <summary>
89
52
/// Gets or sets the child horizontal alignment.
@@ -92,8 +55,8 @@ public class AutoGrid : System.Windows.Controls.Grid
92
55
[ Category ( "Layout" ) , Description ( "Presets the horizontal alignment of all child controls" ) ]
93
56
public HorizontalAlignment ? ChildHorizontalAlignment
94
57
{
95
- get { return ( HorizontalAlignment ? ) GetValue ( ChildHorizontalAlignmentProperty ) ; }
96
- set { SetValue ( ChildHorizontalAlignmentProperty , value ) ; }
58
+ get => ( HorizontalAlignment ? ) GetValue ( ChildHorizontalAlignmentProperty ) ;
59
+ set => SetValue ( ChildHorizontalAlignmentProperty , value ) ;
97
60
}
98
61
99
62
/// <summary>
@@ -103,8 +66,8 @@ public HorizontalAlignment? ChildHorizontalAlignment
103
66
[ Category ( "Layout" ) , Description ( "Presets the margin of all child controls" ) ]
104
67
public Thickness ? ChildMargin
105
68
{
106
- get { return ( Thickness ? ) GetValue ( ChildMarginProperty ) ; }
107
- set { SetValue ( ChildMarginProperty , value ) ; }
69
+ get => ( Thickness ? ) GetValue ( ChildMarginProperty ) ;
70
+ set => SetValue ( ChildMarginProperty , value ) ;
108
71
}
109
72
110
73
/// <summary>
@@ -114,8 +77,8 @@ public Thickness? ChildMargin
114
77
[ Category ( "Layout" ) , Description ( "Presets the vertical alignment of all child controls" ) ]
115
78
public VerticalAlignment ? ChildVerticalAlignment
116
79
{
117
- get { return ( VerticalAlignment ? ) GetValue ( ChildVerticalAlignmentProperty ) ; }
118
- set { SetValue ( ChildVerticalAlignmentProperty , value ) ; }
80
+ get => ( VerticalAlignment ? ) GetValue ( ChildVerticalAlignmentProperty ) ;
81
+ set => SetValue ( ChildVerticalAlignmentProperty , value ) ;
119
82
}
120
83
121
84
/// <summary>
@@ -124,8 +87,8 @@ public VerticalAlignment? ChildVerticalAlignment
124
87
[ Category ( "Layout" ) , Description ( "Defines a set number of columns" ) ]
125
88
public int ColumnCount
126
89
{
127
- get { return ( int ) GetValue ( ColumnCountProperty ) ; }
128
- set { SetValue ( ColumnCountProperty , value ) ; }
90
+ get => ( int ) GetValue ( ColumnCountProperty ) ;
91
+ set => SetValue ( ColumnCountProperty , value ) ;
129
92
}
130
93
131
94
/// <summary>
@@ -134,8 +97,8 @@ public int ColumnCount
134
97
[ Category ( "Layout" ) , Description ( "Defines all columns using comma separated grid length notation" ) ]
135
98
public string Columns
136
99
{
137
- get { return ( string ) GetValue ( ColumnsProperty ) ; }
138
- set { SetValue ( ColumnsProperty , value ) ; }
100
+ get => ( string ) GetValue ( ColumnsProperty ) ;
101
+ set => SetValue ( ColumnsProperty , value ) ;
139
102
}
140
103
141
104
/// <summary>
@@ -144,8 +107,8 @@ public string Columns
144
107
[ Category ( "Layout" ) , Description ( "Presets the width of all columns set using the ColumnCount property" ) ]
145
108
public GridLength ColumnWidth
146
109
{
147
- get { return ( GridLength ) GetValue ( ColumnWidthProperty ) ; }
148
- set { SetValue ( ColumnWidthProperty , value ) ; }
110
+ get => ( GridLength ) GetValue ( ColumnWidthProperty ) ;
111
+ set => SetValue ( ColumnWidthProperty , value ) ;
149
112
}
150
113
151
114
/// <summary>
@@ -158,8 +121,8 @@ public GridLength ColumnWidth
158
121
[ Category ( "Layout" ) , Description ( "Set to false to disable the auto layout functionality" ) ]
159
122
public bool IsAutoIndexing
160
123
{
161
- get { return ( bool ) GetValue ( IsAutoIndexingProperty ) ; }
162
- set { SetValue ( IsAutoIndexingProperty , value ) ; }
124
+ get => ( bool ) GetValue ( IsAutoIndexingProperty ) ;
125
+ set => SetValue ( IsAutoIndexingProperty , value ) ;
163
126
}
164
127
165
128
/// <summary>
@@ -170,8 +133,8 @@ public bool IsAutoIndexing
170
133
[ Category ( "Layout" ) , Description ( "Defines the directionality of the autolayout. Use vertical for a column first layout, horizontal for a row first layout." ) ]
171
134
public Orientation Orientation
172
135
{
173
- get { return ( Orientation ) GetValue ( OrientationProperty ) ; }
174
- set { SetValue ( OrientationProperty , value ) ; }
136
+ get => ( Orientation ) GetValue ( OrientationProperty ) ;
137
+ set => SetValue ( OrientationProperty , value ) ;
175
138
}
176
139
177
140
/// <summary>
@@ -180,8 +143,8 @@ public Orientation Orientation
180
143
[ Category ( "Layout" ) , Description ( "Defines a set number of rows" ) ]
181
144
public int RowCount
182
145
{
183
- get { return ( int ) GetValue ( RowCountProperty ) ; }
184
- set { SetValue ( RowCountProperty , value ) ; }
146
+ get => ( int ) GetValue ( RowCountProperty ) ;
147
+ set => SetValue ( RowCountProperty , value ) ;
185
148
}
186
149
187
150
/// <summary>
@@ -190,8 +153,8 @@ public int RowCount
190
153
[ Category ( "Layout" ) , Description ( "Presets the height of all rows set using the RowCount property" ) ]
191
154
public GridLength RowHeight
192
155
{
193
- get { return ( GridLength ) GetValue ( RowHeightProperty ) ; }
194
- set { SetValue ( RowHeightProperty , value ) ; }
156
+ get => ( GridLength ) GetValue ( RowHeightProperty ) ;
157
+ set => SetValue ( RowHeightProperty , value ) ;
195
158
}
196
159
197
160
/// <summary>
@@ -200,14 +163,10 @@ public GridLength RowHeight
200
163
[ Category ( "Layout" ) , Description ( "Defines all rows using comma separated grid length notation" ) ]
201
164
public string Rows
202
165
{
203
- get { return ( string ) GetValue ( RowsProperty ) ; }
204
- set { SetValue ( RowsProperty , value ) ; }
166
+ get => ( string ) GetValue ( RowsProperty ) ;
167
+ set => SetValue ( RowsProperty , value ) ;
205
168
}
206
169
207
- #endregion Public Properties
208
-
209
- #region Public Methods
210
-
211
170
/// <summary>
212
171
/// Handles the column count changed event
213
172
/// </summary>
@@ -351,10 +310,6 @@ public static void RowsChanged(DependencyObject d, DependencyPropertyChangedEven
351
310
grid . RowDefinitions . Add ( new RowDefinition ( ) { Height = def } ) ;
352
311
}
353
312
354
- #endregion Public Methods
355
-
356
- #region Protected Methods
357
-
358
313
/// <summary>
359
314
/// Measures the children of a <see cref="T:System.Windows.Controls.Grid"/> in anticipation of arranging them during the <see cref="M:ArrangeOverride"/> pass.
360
315
/// </summary>
@@ -368,10 +323,6 @@ protected override Size MeasureOverride(Size constraint)
368
323
return base . MeasureOverride ( constraint ) ;
369
324
}
370
325
371
- #endregion Protected Methods
372
-
373
- #region Private Methods
374
-
375
326
/// <summary>
376
327
/// Called when [child horizontal alignment changed].
377
328
/// </summary>
@@ -455,9 +406,9 @@ private int Clamp(int value, int max)
455
406
/// </summary>
456
407
private void PerformLayout ( )
457
408
{
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 ;
461
412
462
413
if ( rowCount == 0 || colCount == 0 )
463
414
return ;
@@ -516,14 +467,10 @@ private void PerformLayout()
516
467
ApplyChildLayout ( child ) ;
517
468
}
518
469
}
519
-
520
- #endregion Private Methods
521
470
}
522
471
523
- public static class DependencyExtensions
472
+ file static class DependencyExtensions
524
473
{
525
- #region Public Methods
526
-
527
474
/// <summary>
528
475
/// Sets the value of the <paramref name="property"/> only if it hasn't been explicitly set.
529
476
/// </summary>
@@ -538,6 +485,4 @@ public static bool SetIfDefault<T>(this DependencyObject o, DependencyProperty p
538
485
539
486
return false ;
540
487
}
541
-
542
- #endregion Public Methods
543
- }
488
+ }
0 commit comments