Skip to content

Commit

Permalink
Binding list insert issue. Fixes #507
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandPheasant committed May 4, 2022
1 parent 3eb51bd commit cd2d739
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
22 changes: 18 additions & 4 deletions src/DynamicData.Tests/Binding/BindingListToChangeSetFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ public void Add()
_results.Data.Items.First().Should().Be(1);
}

public void Dispose()
{
_results.Dispose();
}
public void Dispose() => _results.Dispose();

[Fact]
public void Duplicates()
Expand Down Expand Up @@ -129,6 +126,23 @@ public void ResetFiresClearsAndAdds()
resetNotification.Adds.Should().Be(10);
}


[Fact]
public void InsertInto()
{
//Fixes https://github.com/reactivemarbles/DynamicData/issues/507
var target = new ObservableCollectionExtended<string>();

var bindingList = new BindingList<string>() { "a", "b", "c", "d" };
bindingList.ToObservableChangeSet()
.Bind(target)
.Subscribe();

bindingList.Insert(2, "Z at index 2");

target.Should().BeEquivalentTo("a", "b", "Z at index 2", "c", "d");
}

private class TestBindingList<T> : BindingList<T>
{
public void Reset()
Expand Down
10 changes: 9 additions & 1 deletion src/DynamicData/Binding/BindingListEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@ public static IObservable<IChangeSet<T>> ToObservableChangeSet<TCollection, T>(t
{
case ListChangedType.ItemAdded when source[changes.NewIndex] is T newItem:
{
list.Add(newItem);
if (changes.NewIndex == -1)
{
list.Add(newItem);
}
else
{
list.Insert(changes.NewIndex, newItem);
}
break;
}
Expand Down
10 changes: 2 additions & 8 deletions src/DynamicData/List/ListEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,7 @@ public static int BinarySearch<TItem, TSearch>(this IList<TItem> list, TSearch v
/// or
/// changes.
/// </exception>
public static void Clone<T>(this IList<T> source, IChangeSet<T> changes)
{
Clone(source, changes, null);
}
public static void Clone<T>(this IList<T> source, IChangeSet<T> changes) => Clone(source, changes, null);

/// <summary>
/// Clones the list from the specified change set.
Expand All @@ -268,10 +265,7 @@ public static void Clone<T>(this IList<T> source, IChangeSet<T> changes)
/// or
/// changes.
/// </exception>
public static void Clone<T>(this IList<T> source, IChangeSet<T> changes, IEqualityComparer<T>? equalityComparer)
{
Clone(source, (IEnumerable<Change<T>>)changes, equalityComparer);
}
public static void Clone<T>(this IList<T> source, IChangeSet<T> changes, IEqualityComparer<T>? equalityComparer) => Clone(source, (IEnumerable<Change<T>>)changes, equalityComparer);

/// <summary>
/// Clones the list from the specified enumerable of changes.
Expand Down

0 comments on commit cd2d739

Please sign in to comment.