1
1
using Microsoft . UI . Xaml ;
2
2
using System ;
3
3
using System . Collections . Generic ;
4
- using System . Collections . ObjectModel ;
5
4
using System . Collections . Specialized ;
6
- using System . ComponentModel ;
7
5
using System . Linq ;
6
+ using Windows . Foundation . Collections ;
8
7
9
8
namespace WinUI . TableView ;
10
9
11
10
/// <summary>
12
- /// Represents a collection of columns in a TableView.
11
+ /// Represents a collection of <see cref="TableViewColumn"/> objects used in a <see cref="WinUI. TableView.TableView"/> .
13
12
/// </summary>
14
- public partial class TableViewColumnsCollection : ObservableCollection < TableViewColumn >
13
+ /// <remarks>This collection provides functionality for managing columns in a <see cref="WinUI.TableView.TableView"/>, including adding,
14
+ /// removing, and tracking changes to column properties. It supports notifications for collection changes and column
15
+ /// property changes, enabling dynamic updates to the <see cref="WinUI.TableView.TableView"/>.</remarks>
16
+ public partial class TableViewColumnsCollection : DependencyObjectCollection , ITableViewColumnsCollection
15
17
{
18
+ private TableViewColumn [ ] _itemsCopy = [ ] ; // To keep a copy of the items to keep track of removed items
19
+ /// <inheritdoc/>
20
+ public event EventHandler < TableViewColumnPropertyChangedEventArgs > ? ColumnPropertyChanged ;
21
+ /// <inheritdoc/>
22
+ public event NotifyCollectionChangedEventHandler ? CollectionChanged ;
23
+
16
24
/// <summary>
17
- /// Occurs when a property of a column in the collection changes .
25
+ /// The constructor for the <see cref="TableViewColumnsCollection"/> class .
18
26
/// </summary>
19
- internal event EventHandler < TableViewColumnPropertyChangedEventArgs > ? ColumnPropertyChanged ;
27
+ /// <param name="tableView">
28
+ /// The <see cref="WinUI.TableView.TableView"/> that owns this collection.
29
+ /// </param>
30
+ public TableViewColumnsCollection ( TableView tableView )
31
+ {
32
+ TableView = tableView ?? throw new ArgumentNullException ( nameof ( tableView ) ) ;
33
+ VectorChanged += OnVectorChanged ;
34
+ }
20
35
21
36
/// <summary>
22
- /// Gets the list of visible columns in the collection .
37
+ /// Handles changes to the underlying vector of <see cref="DependencyObject"/> items .
23
38
/// </summary>
24
- internal IList < TableViewColumn > VisibleColumns =>
25
- [ .. Items . Where ( x => x . Visibility == Visibility . Visible )
26
- . OrderBy ( x => x . Order ?? 0 ) ] ;
27
-
28
- /// <inheritdoc/>
29
- protected override void OnCollectionChanged ( NotifyCollectionChangedEventArgs e )
39
+ private void OnVectorChanged ( IObservableVector < DependencyObject > sender , IVectorChangedEventArgs args )
30
40
{
31
- base . OnCollectionChanged ( e ) ;
41
+ var index = ( int ) args . Index ;
32
42
33
- if ( e . NewItems != null )
43
+ switch ( args . CollectionChange )
34
44
{
35
- foreach ( var column in e . NewItems . OfType < TableViewColumn > ( ) )
36
- {
37
- column . SetOwningCollection ( this ) ;
38
- column . SetOwningTableView ( TableView ! ) ;
39
- }
45
+ case CollectionChange . ItemInserted :
46
+ if ( args . Index < Count )
47
+ {
48
+ var column = ( TableViewColumn ) sender [ index ] ;
49
+ column . SetOwningCollection ( this ) ;
50
+ column . SetOwningTableView ( ( ( ITableViewColumnsCollection ) this ) . TableView ! ) ;
51
+ CollectionChanged ? . Invoke ( this , new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Add , column , ( int ) args . Index ) ) ;
52
+ }
53
+ break ;
54
+ case CollectionChange . ItemRemoved :
55
+ if ( args . Index < _itemsCopy . Length )
56
+ {
57
+ var column = _itemsCopy [ index ] ;
58
+ column . SetOwningCollection ( null ! ) ;
59
+ column . SetOwningTableView ( null ! ) ;
60
+ CollectionChanged ? . Invoke ( this , new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Remove , column , ( int ) args . Index ) ) ;
61
+ }
62
+ break ;
63
+ case CollectionChange . Reset :
64
+ foreach ( var item in _itemsCopy )
65
+ {
66
+ item . SetOwningCollection ( null ! ) ;
67
+ item . SetOwningTableView ( null ! ) ;
68
+ CollectionChanged ? . Invoke ( this , new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Reset ) ) ;
69
+ }
70
+ break ;
40
71
}
41
72
42
- if ( e . OldItems != null )
43
- {
44
- foreach ( var column in e . OldItems . OfType < TableViewColumn > ( ) )
45
- {
46
- column . SetOwningCollection ( null ! ) ;
47
- column . SetOwningTableView ( null ! ) ;
48
- }
49
- }
73
+ _itemsCopy = new TableViewColumn [ Count ] ;
74
+ CopyTo ( _itemsCopy , 0 ) ;
50
75
}
51
76
52
77
/// <summary>
53
78
/// Handles the property changed event for a column.
54
79
/// </summary>
55
- /// <param name="column">The column that changed.</param>
56
- /// <param name="propertyName">The name of the property that changed.</param>
57
80
internal void HandleColumnPropertyChanged ( TableViewColumn column , string propertyName )
58
81
{
59
- if ( Items . Contains ( column ) )
82
+ if ( Contains ( column ) && this is ITableViewColumnsCollection d )
60
83
{
61
84
var index = IndexOf ( column ) ;
62
85
ColumnPropertyChanged ? . Invoke ( this , new TableViewColumnPropertyChangedEventArgs ( column , propertyName , index ) ) ;
63
86
}
64
87
}
65
88
66
- /// <summary>
67
- /// Gets or sets the TableView associated with the collection.
68
- /// </summary>
69
- public TableView ? TableView { get ; internal set ; }
70
- }
89
+ /// <inheritdoc/>
90
+ public TableView ? TableView { get ; }
91
+
92
+ /// <inheritdoc/>
93
+ public IList < TableViewColumn > VisibleColumns => [ .. this . OfType < TableViewColumn > ( )
94
+ . Where ( x => x . Visibility == Visibility . Visible )
95
+ . OrderBy ( x => x . Order ?? 0 ) ] ;
96
+
97
+ TableViewColumn IList < TableViewColumn > . this [ int index ]
98
+ {
99
+ get => ( TableViewColumn ) base [ index ] ;
100
+ set => base [ index ] = value ;
101
+ }
102
+
103
+ int ICollection < TableViewColumn > . Count => Count ;
104
+
105
+ bool ICollection < TableViewColumn > . IsReadOnly => IsReadOnly ;
106
+
107
+ void ICollection < TableViewColumn > . Add ( TableViewColumn item )
108
+ {
109
+ Add ( item ) ;
110
+ }
111
+
112
+ void ICollection < TableViewColumn > . Clear ( )
113
+ {
114
+ Clear ( ) ;
115
+ }
116
+
117
+ bool ICollection < TableViewColumn > . Contains ( TableViewColumn item )
118
+ {
119
+ return Contains ( item ) ;
120
+ }
121
+
122
+ void ICollection < TableViewColumn > . CopyTo ( TableViewColumn [ ] array , int arrayIndex )
123
+ {
124
+ CopyTo ( array , arrayIndex ) ;
125
+ }
126
+
127
+ IEnumerator < TableViewColumn > IEnumerable < TableViewColumn > . GetEnumerator ( )
128
+ {
129
+ foreach ( var item in this )
130
+ {
131
+ yield return ( TableViewColumn ) item ;
132
+ }
133
+ }
134
+
135
+ int IList < TableViewColumn > . IndexOf ( TableViewColumn item )
136
+ {
137
+ return IndexOf ( item ) ;
138
+ }
139
+
140
+ void IList < TableViewColumn > . Insert ( int index , TableViewColumn item )
141
+ {
142
+ Insert ( index , item ) ;
143
+ }
144
+
145
+ bool ICollection < TableViewColumn > . Remove ( TableViewColumn item )
146
+ {
147
+ var index = IndexOf ( item ) ;
148
+
149
+ if ( index >= 0 )
150
+ {
151
+ RemoveAt ( index ) ;
152
+ return true ;
153
+ }
154
+
155
+ return false ;
156
+ }
157
+
158
+ void IList < TableViewColumn > . RemoveAt ( int index )
159
+ {
160
+ RemoveAt ( index ) ;
161
+ }
162
+ }
0 commit comments