@@ -164,32 +164,44 @@ public class EtoTableViewDataSource : NSTableViewDataSource
164
164
{
165
165
WeakReference handler ;
166
166
167
- public GridViewHandler Handler { get { return ( GridViewHandler ) ( handler != null ? handler . Target : null ) ; } set { handler = new WeakReference ( value ) ; } }
167
+ public GridViewHandler Handler { get => ( GridViewHandler ) handler ? . Target ; set => handler = new WeakReference ( value ) ; }
168
168
169
- public override nint GetRowCount ( NSTableView tableView )
170
- {
171
- return ( Handler . collection != null && Handler . collection . Collection != null ) ? Handler . collection . Count : 0 ;
172
- }
169
+ public override nint GetRowCount ( NSTableView tableView ) => Handler ? . collection ? . Count ?? 0 ;
173
170
174
171
public override NSObject GetObjectValue ( NSTableView tableView , NSTableColumn tableColumn , nint row )
175
172
{
176
- var item = Handler . collection . ElementAt ( ( int ) row ) ;
177
- var colHandler = Handler . GetColumn ( tableColumn ) ;
178
- return colHandler == null ? null : colHandler . GetObjectValue ( item ) ;
173
+ var h = Handler ;
174
+ if ( h == null )
175
+ return null ;
176
+
177
+ if ( row >= h . collection . Count )
178
+ {
179
+ // re-jig as we're off somehow.. usually because of some programming error, but let's be nice and not actually crash.
180
+ tableView . ReloadData ( ) ;
181
+ return null ;
182
+ }
183
+ var colHandler = h . GetColumn ( tableColumn ) ;
184
+ var item = h . collection . ElementAt ( ( int ) row ) ;
185
+ return colHandler ? . GetObjectValue ( item ) ;
179
186
}
180
187
181
188
public override void SetObjectValue ( NSTableView tableView , NSObject theObject , NSTableColumn tableColumn , nint row )
182
189
{
183
- if ( row >= Handler . collection . Count )
190
+ var h = Handler ;
191
+ if ( h == null )
184
192
return ;
185
- var item = Handler . collection . ElementAt ( ( int ) row ) ;
186
- var colHandler = Handler . GetColumn ( tableColumn ) ;
187
- if ( colHandler != null && Handler . SuppressUpdate == 0 )
193
+
194
+ if ( row >= h . collection . Count )
195
+ return ;
196
+
197
+ var item = h . collection . ElementAt ( ( int ) row ) ;
198
+ var colHandler = h . GetColumn ( tableColumn ) ;
199
+ if ( colHandler != null && h . SuppressUpdate == 0 )
188
200
{
189
201
colHandler . SetObjectValue ( item , theObject ) ;
190
202
191
- Handler . SetIsEditing ( false ) ;
192
- Handler . Callback . OnCellEdited ( Handler . Widget , new GridViewCellEventArgs ( colHandler . Widget , ( int ) row , colHandler . Column , item ) ) ;
203
+ h . SetIsEditing ( false ) ;
204
+ h . Callback . OnCellEdited ( h . Widget , new GridViewCellEventArgs ( colHandler . Widget , ( int ) row , colHandler . Column , item ) ) ;
193
205
}
194
206
}
195
207
@@ -590,13 +602,19 @@ public override void RemoveAllItems()
590
602
591
603
public IEnumerable < object > DataStore
592
604
{
593
- get { return collection != null ? collection . Collection : null ; }
605
+ get => collection ? . Collection ;
594
606
set
595
607
{
596
608
if ( collection != null )
609
+ {
610
+ if ( ReferenceEquals ( collection . Collection , value ) )
611
+ return ;
597
612
collection . Unregister ( ) ;
613
+ }
598
614
collection = new CollectionHandler { Handler = this } ;
599
615
collection . Register ( value ) ;
616
+ Control . ReloadData ( ) ;
617
+ AutoSizeColumns ( true ) ;
600
618
ResetAutoSizedColumns ( ) ;
601
619
InvalidateMeasure ( ) ;
602
620
}
0 commit comments