Skip to content

Commit a10614c

Browse files
authored
Merge pull request #2339 from cwensley/curtis/mac-gridview-reload-count
Mac: GridView.DataSource fixes
2 parents 49f15e6 + 8064ce2 commit a10614c

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/Eto.Mac/Forms/Controls/GridViewHandler.cs

+33-15
Original file line numberDiff line numberDiff line change
@@ -164,32 +164,44 @@ public class EtoTableViewDataSource : NSTableViewDataSource
164164
{
165165
WeakReference handler;
166166

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); }
168168

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;
173170

174171
public override NSObject GetObjectValue(NSTableView tableView, NSTableColumn tableColumn, nint row)
175172
{
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);
179186
}
180187

181188
public override void SetObjectValue(NSTableView tableView, NSObject theObject, NSTableColumn tableColumn, nint row)
182189
{
183-
if (row >= Handler.collection.Count)
190+
var h = Handler;
191+
if (h == null)
184192
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)
188200
{
189201
colHandler.SetObjectValue(item, theObject);
190202

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));
193205
}
194206
}
195207

@@ -590,13 +602,19 @@ public override void RemoveAllItems()
590602

591603
public IEnumerable<object> DataStore
592604
{
593-
get { return collection != null ? collection.Collection : null; }
605+
get => collection?.Collection;
594606
set
595607
{
596608
if (collection != null)
609+
{
610+
if (ReferenceEquals(collection.Collection, value))
611+
return;
597612
collection.Unregister();
613+
}
598614
collection = new CollectionHandler { Handler = this };
599615
collection.Register(value);
616+
Control.ReloadData();
617+
AutoSizeColumns(true);
600618
ResetAutoSizedColumns();
601619
InvalidateMeasure();
602620
}

src/Eto/CollectionChangedHandler.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ public virtual void Unregister()
7676
{
7777
notify.CollectionChanged -= CollectionChanged;
7878
}
79-
OnUnregisterCollection(EventArgs.Empty);
80-
8179
Collection = null;
80+
81+
OnUnregisterCollection(EventArgs.Empty);
8282
}
8383

8484
/// <summary>

0 commit comments

Comments
 (0)