diff --git a/Toolkit.Foundation/ObservableCollection.cs b/Toolkit.Foundation/ObservableCollection.cs index 763ca7c..ba218c5 100644 --- a/Toolkit.Foundation/ObservableCollection.cs +++ b/Toolkit.Foundation/ObservableCollection.cs @@ -144,13 +144,22 @@ public partial class ObservableCollection : } } - public void SetSource(IList source) + private Func defaultSelectionFactory; + + public void SetSource(IList source, + Func? defaultSelectionFactory) { foreach (TItem item in source) { Add(item); } + if (defaultSelectionFactory is not null) + { + this.defaultSelectionFactory = defaultSelectionFactory; + SelectedItem = defaultSelectionFactory.Invoke(); + } + if (source is INotifyCollectionChanged observableSource) { observableSource.CollectionChanged -= SourceCollectionChanged; @@ -193,6 +202,11 @@ public partial class ObservableCollection : { Add(item); } + + if (defaultSelectionFactory is not null) + { + SelectedItem = defaultSelectionFactory.Invoke(); + } } break; } @@ -266,6 +280,8 @@ public partial class ObservableCollection : public void Reset(Action> factory, bool disposeItems = true) { + SelectedItem = default; + Clear(disposeItems); factory.Invoke(this); }