Skip to content

Commit

Permalink
Make BindingList adaptors replace the changed item instead of re-addi…
Browse files Browse the repository at this point in the history
…ng (#694)
  • Loading branch information
Metadorius authored Apr 7, 2023
1 parent 82c9639 commit d1ca572
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
23 changes: 9 additions & 14 deletions src/DynamicData/Binding/BindingListAdaptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,18 @@ private static void DoUpdate(IChangeSet<TObject, TKey> changes, BindingList<TObj
break;

case ChangeReason.Update:
var item = update.Previous.Value;

if (item is not null)
{
list.Remove(item);
}

list.Add(update.Current);
var previousIndex = list.IndexOf(update.Previous.Value);
if (previousIndex >= 0)
list[previousIndex] = update.Current;
else
list.Add(update.Current);
break;

case ChangeReason.Refresh:
{
var index = list.IndexOf(update.Current);
if (index != -1)
list.ResetItem(index);
break;
}
var index = list.IndexOf(update.Current);
if (index != -1)
list.ResetItem(index);
break;
}
}
}
Expand Down
26 changes: 18 additions & 8 deletions src/DynamicData/Binding/SortedBindingListAdaptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,27 @@ private void DoUpdate(ISortedChangeSet<TObject, TKey> changes)
break;

case ChangeReason.Update:
_list.RemoveAt(change.PreviousIndex);
_list.Insert(change.CurrentIndex, change.Current);
if (change.CurrentIndex != change.PreviousIndex)
{
_list.RemoveAt(change.PreviousIndex);
_list.Insert(change.CurrentIndex, change.Current);
}
else
{
var previousIndex = _list.IndexOf(change.Previous.Value);
if (previousIndex >= 0)
_list[previousIndex] = change.Current;
else
_list.Add(change.Current);
}

break;

case ChangeReason.Refresh:
{
var index = _list.IndexOf(change.Current);
if (index != -1)
_list.ResetItem(index);
break;
}
var index = _list.IndexOf(change.Current);
if (index != -1)
_list.ResetItem(index);
break;
}
}
}
Expand Down

0 comments on commit d1ca572

Please sign in to comment.