Table of Contents

Class ObservableCollectionExtensions

Namespace
WindowSill.API
Assembly
WindowSill.API.dll

Provides extension methods for ObservableCollection<T>.

public static class ObservableCollectionExtensions
Inheritance
ObservableCollectionExtensions
Inherited Members

Methods

IndexOf<T>(ObservableCollection<T>, Predicate<T>)

Searches for the first element in the collection that matches the specified predicate and returns its index.

public static int IndexOf<T>(this ObservableCollection<T> collection, Predicate<T> predicate)

Parameters

collection ObservableCollection<T>

The ObservableCollection<T> to search.

predicate Predicate<T>

The Predicate<T> used to evaluate each element in the collection.

Returns

int

The zero-based index of the first element that matches the predicate; otherwise, -1 if no matching element is found.

Type Parameters

T

SynchronizeWithAsync<T, U>(ObservableCollection<T>, IEnumerable<U>, Func<T, U, bool>?, Func<U, Task<T>>)

Synchronizes the contents of the ObservableCollection<T> with the specified collection of new items. It essentially performs an incremental update to the origin collection based on the newItems.

public static Task SynchronizeWithAsync<T, U>(this ObservableCollection<T> origin, IEnumerable<U> newItems, Func<T, U, bool>? comparer, Func<U, Task<T>> convertToT)

Parameters

origin ObservableCollection<T>

The ObservableCollection<T> to be synchronized.

newItems IEnumerable<U>

The collection of new items to synchronize with. If null, the origin collection will be cleared.

comparer Func<T, U, bool>

A function that compares items of type T from the original list with an item of type U from the new list.

convertToT Func<U, Task<T>>

A function that converts an item of type U to type T.

Returns

Task

Type Parameters

T

The type of elements in the ObservableCollection<T>.

U

The type of elements in the newItems collection.

Remarks

Note: This method is asynchronous and is not thread-safe. This method performs the following operations to synchronize the origin collection:

  • Removes items from origin that are not present in newItems.
  • Moves items within origin to match the order of newItems.
  • Inserts new items from newItems into origin at the appropriate positions.
The comparison between items in origin and newItems is based on the Equals(object) method.

SynchronizeWith<T>(ObservableCollection<T>, IEnumerable<T>)

Updates the contents of the specified ObservableCollection<T> to match the items in the provided sequence. It essentially performs an incremental update to the origin collection based on the newItems.

public static void SynchronizeWith<T>(this ObservableCollection<T> origin, IEnumerable<T> newItems)

Parameters

origin ObservableCollection<T>

The ObservableCollection<T> to be updated.

newItems IEnumerable<T>

The sequence of items to synchronize with the collection.

Type Parameters

T

The type of elements in the collection.

Remarks

This method performs the following operations to synchronize the collection:

  1. Removes items from origin that are not present in newItems.
  2. Moves items within origin to match the order in newItems.
  3. Inserts items from newItems into origin if they are not already present.
The method ensures that the final state of origin matches the order and contents of newItems.

SynchronizeWith<T, U>(ObservableCollection<T>, IEnumerable<U>, Func<T, U, bool>?, Func<U, T>)

Synchronizes the contents of the ObservableCollection<T> with the specified collection of new items. It essentially performs an incremental update to the origin collection based on the newItems.

public static void SynchronizeWith<T, U>(this ObservableCollection<T> origin, IEnumerable<U> newItems, Func<T, U, bool>? comparer, Func<U, T> convertToT)

Parameters

origin ObservableCollection<T>

The ObservableCollection<T> to be synchronized.

newItems IEnumerable<U>

The collection of new items to synchronize with. If null, the origin collection will be cleared.

comparer Func<T, U, bool>

A function that compares items of type T from the original list with an item of type U from the new list.

convertToT Func<U, T>

A function that converts an item of type U to type T.

Type Parameters

T

The type of elements in the ObservableCollection<T>.

U

The type of elements in the newItems collection.

Remarks

This method performs the following operations to synchronize the origin collection:

  • Removes items from origin that are not present in newItems.
  • Moves items within origin to match the order of newItems.
  • Inserts new items from newItems into origin at the appropriate positions.
The comparison between items in origin and newItems is based on the Equals(object) method.