ObservableCollection Concatenation

Collection: ConcatObservatorCollection<T>
Extension method: ObservatorCollectionExtensions.Concat(first, second)

Description

This collection listens to two IObservableCollection<T> and returns the combined resulting items.

Example

The following example shows how to combine a list of folders and files with a common interface in a single collection.

using ZumtenSoft.WpfUtils.Collections;

public interface IFileSystemObject
{
    string Name { get; }
}

public class Folder : IFileSystemObject
{
    public Folder(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }
}

public class File : IFileSystemObject
{
    public File(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }
}

You can then create two different lists and combine them together using Concat

IObservableCollection<Folder> folders = new ObservableCollection<Folder>();
IObservableCollection<File> files = new ObservableCollection<File>();
IObservableCollection<IFileSystemObject> items =
    folders.Cast<IFileSystemObject>()
    .Concat(files.Cast<IFileSystemObject>());

folders.Add(new Folder("FolderName"));
files.Add(new File("FileName"));

Last edited May 19, 2013 at 10:43 PM by Zumten, version 3

Comments

No comments yet.