22using System . Windows ;
33using System . Windows . Controls ;
44
5- namespace One . Control . AttachDP
5+ namespace One . Control . AttachDP ;
6+
7+ public class GridAttach
68{
7- public class GridAttach
8- {
9- public static readonly DependencyProperty NameProperty = DependencyProperty . RegisterAttached (
10- "Name" , typeof ( string ) , typeof ( GridAttach ) , new PropertyMetadata ( default ( string ) ) ) ;
9+ public static readonly DependencyProperty NameProperty = DependencyProperty . RegisterAttached (
10+ "Name" , typeof ( string ) , typeof ( GridAttach ) , new PropertyMetadata ( default ( string ) ) ) ;
1111
12- public static void SetName ( DependencyObject element , string value )
13- {
14- element . SetValue ( NameProperty , value ) ;
15- }
12+ public static void SetName ( DependencyObject element , string value )
13+ {
14+ element . SetValue ( NameProperty , value ) ;
15+ }
1616
17- public static string GetName ( DependencyObject element )
18- {
19- return ( string ) element . GetValue ( NameProperty ) ;
20- }
17+ public static string GetName ( DependencyObject element )
18+ {
19+ return ( string ) element . GetValue ( NameProperty ) ;
20+ }
2121
22- public static readonly DependencyProperty RowNameProperty = DependencyProperty . RegisterAttached (
23- "RowName" , typeof ( string ) , typeof ( GridAttach ) ,
24- new PropertyMetadata ( default ( string ) , RowName_PropertyChanged ) ) ;
22+ public static readonly DependencyProperty RowNameProperty = DependencyProperty . RegisterAttached (
23+ "RowName" , typeof ( string ) , typeof ( GridAttach ) ,
24+ new PropertyMetadata ( default ( string ) , RowName_PropertyChanged ) ) ;
2525
26- private static void RowName_PropertyChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
26+ private static void RowName_PropertyChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
27+ {
28+ if ( d is FrameworkElement frameworkElement )
2729 {
28- if ( d is FrameworkElement frameworkElement )
30+ if ( e . NewValue is string rowName )
2931 {
30- if ( e . NewValue is string rowName )
32+ if ( string . IsNullOrEmpty ( rowName ) )
3133 {
32- if ( string . IsNullOrEmpty ( rowName ) )
33- {
34- return ;
35- }
34+ return ;
35+ }
3636
37- if ( frameworkElement . Parent is Grid grid )
37+ if ( frameworkElement . Parent is Grid grid )
38+ {
39+ for ( var i = 0 ; i < grid . RowDefinitions . Count ; i ++ )
3840 {
39- for ( var i = 0 ; i < grid . RowDefinitions . Count ; i ++ )
41+ var gridRowDefinition = grid . RowDefinitions [ i ] ;
42+ var gridRowName = GetName ( gridRowDefinition ) ;
43+ if ( ! string . IsNullOrEmpty ( gridRowName ) &&
44+ gridRowName . Equals ( rowName , StringComparison . Ordinal ) )
4045 {
41- var gridRowDefinition = grid . RowDefinitions [ i ] ;
42- var gridRowName = GetName ( gridRowDefinition ) ;
43- if ( ! string . IsNullOrEmpty ( gridRowName ) &&
44- gridRowName . Equals ( rowName , StringComparison . Ordinal ) )
45- {
46- Grid . SetRow ( frameworkElement , i ) ;
47- return ;
48- }
46+ Grid . SetRow ( frameworkElement , i ) ;
47+ return ;
4948 }
5049 }
51- else
52- {
53- throw new ArgumentException ( "只有在Grid容器内才能设置 RowName 附加属性" ) ;
54- }
50+ }
51+ else
52+ {
53+ throw new ArgumentException ( "只有在Grid容器内才能设置 RowName 附加属性" ) ;
5554 }
5655 }
5756 }
57+ }
5858
59- public static void SetRowName ( DependencyObject element , string value )
60- {
61- element . SetValue ( RowNameProperty , value ) ;
62- }
59+ public static void SetRowName ( DependencyObject element , string value )
60+ {
61+ element . SetValue ( RowNameProperty , value ) ;
62+ }
6363
64- public static string GetRowName ( DependencyObject element )
65- {
66- return ( string ) element . GetValue ( RowNameProperty ) ;
67- }
64+ public static string GetRowName ( DependencyObject element )
65+ {
66+ return ( string ) element . GetValue ( RowNameProperty ) ;
67+ }
6868
69- public static readonly DependencyProperty ColumnNameProperty = DependencyProperty . RegisterAttached (
70- "ColumnName" , typeof ( string ) , typeof ( GridAttach ) ,
71- new PropertyMetadata ( default ( string ) , ColumnName_PropertyChanged ) ) ;
69+ public static readonly DependencyProperty ColumnNameProperty = DependencyProperty . RegisterAttached (
70+ "ColumnName" , typeof ( string ) , typeof ( GridAttach ) ,
71+ new PropertyMetadata ( default ( string ) , ColumnName_PropertyChanged ) ) ;
7272
73- private static void ColumnName_PropertyChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
73+ private static void ColumnName_PropertyChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
74+ {
75+ if ( d is FrameworkElement frameworkElement )
7476 {
75- if ( d is FrameworkElement frameworkElement )
77+ if ( e . NewValue is string columnName )
7678 {
77- if ( e . NewValue is string columnName )
79+ if ( string . IsNullOrEmpty ( columnName ) )
7880 {
79- if ( string . IsNullOrEmpty ( columnName ) )
80- {
81- return ;
82- }
81+ return ;
82+ }
8383
84- if ( frameworkElement . Parent is Grid grid )
84+ if ( frameworkElement . Parent is Grid grid )
85+ {
86+ for ( var i = 0 ; i < grid . ColumnDefinitions . Count ; i ++ )
8587 {
86- for ( var i = 0 ; i < grid . ColumnDefinitions . Count ; i ++ )
88+ var gridColumnDefinition = grid . ColumnDefinitions [ i ] ;
89+ var gridColumnName = GetName ( gridColumnDefinition ) ;
90+ if ( ! string . IsNullOrEmpty ( gridColumnName ) &&
91+ gridColumnName . Equals ( columnName , StringComparison . Ordinal ) )
8792 {
88- var gridColumnDefinition = grid . ColumnDefinitions [ i ] ;
89- var gridColumnName = GetName ( gridColumnDefinition ) ;
90- if ( ! string . IsNullOrEmpty ( gridColumnName ) &&
91- gridColumnName . Equals ( columnName , StringComparison . Ordinal ) )
92- {
93- Grid . SetColumn ( frameworkElement , i ) ;
94- return ;
95- }
93+ Grid . SetColumn ( frameworkElement , i ) ;
94+ return ;
9695 }
9796 }
98- else
99- {
100- throw new ArgumentException ( "只有在Grid容器内才能设置 ColumnName 附加属性" ) ;
101- }
97+ }
98+ else
99+ {
100+ throw new ArgumentException ( "只有在Grid容器内才能设置 ColumnName 附加属性" ) ;
102101 }
103102 }
104103 }
104+ }
105105
106- public static void SetColumnName ( DependencyObject element , string value )
107- {
108- element . SetValue ( ColumnNameProperty , value ) ;
109- }
106+ public static void SetColumnName ( DependencyObject element , string value )
107+ {
108+ element . SetValue ( ColumnNameProperty , value ) ;
109+ }
110110
111- public static string GetColumnName ( DependencyObject element )
112- {
113- return ( string ) element . GetValue ( ColumnNameProperty ) ;
114- }
111+ public static string GetColumnName ( DependencyObject element )
112+ {
113+ return ( string ) element . GetValue ( ColumnNameProperty ) ;
115114 }
116115}
0 commit comments