Skip to content

Simplify.Wpf

Alexanderius edited this page Jun 27, 2026 · 1 revision

Simplify.Wpf Documentation

Provides extensions for the WPF PresentationFramework, focused on multi-thread-safe observable collections.

Available at NuGet as binary package

Targets net48. Depends on Simplify.System (uses IConcurrentResource).

ObservableCollection Extensions

// Enables multi-thread synchronization of an ObservableCollection instance so it can be safely
// updated from background threads while bound to the UI. An optional lock object can be supplied.
// Throws ArgumentNullException if the collection is null.
public static ObservableCollection<T> EnableSynchronization<T>(
    this ObservableCollection<T> collection, object? lockObject = null);

Usage Example

var items = new ObservableCollection<string>().EnableSynchronization();

// Safe to update from a background thread while bound to a WPF control
await Task.Run(() => items.Add("value"));

SynchronizedObservableCollection

A multi-thread synchronized ObservableCollection<T> that implements IConcurrentResource. Synchronization is enabled automatically on construction.

public class SynchronizedObservableCollection<T> : ObservableCollection<T>, IConcurrentResource
{
    public SynchronizedObservableCollection();
    public SynchronizedObservableCollection(List<T> list);
    public SynchronizedObservableCollection(IEnumerable<T> collection);

    // Invokes the action under the collection's internal lock.
    public void InvokeConcurrently(Action action);
}

Usage Example

var people = new SynchronizedObservableCollection<Person>();

// Bind to a WPF ItemsControl and update from any thread
await Task.Run(() => people.Add(new Person("Alice")));

// Perform a compound operation atomically
people.InvokeConcurrently(() =>
{
    people.Clear();
    people.Add(new Person("Bob"));
});

Clone this wiki locally