Shashika Nanayakkara
Shashika Nanayakkara

Reputation: 11

How can I maintain two observable collection with same items in WPF..?

I have this observable collection where I add my items and bind with "Listbox" in the xaml. But I want to maintain a clone of this observable collection and bind that clone observable collection into that "Listbox" instead of original Observable Collection and add items into that cloned observable collection first and update Original Observable Collection with a button click. I'm using MVVM Light for by application.

Any help for this.? This is my original observable collection.

public ObservableCollection<ColumnNameDefinition> HistoricColumns { get; set; }

Upvotes: 0

Views: 1396

Answers (1)

Benedikt Schroeder
Benedikt Schroeder

Reputation: 81

I have not tested this code but it should work just fine. I have hooked up the CollectionChanged event on the clone and i am maintaining a collection of changes. Then if you want to commit the changes just execute the CommitChangesCommand and all changes are added to the source.

using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Input;

namespace WpfApplication1
{
public class ViewModel
{
    private readonly ObservableCollection<Item> _clone;

    private readonly ObservableCollection<Item> _source;

    private readonly Collection<Item> _changes = new Collection<Item>();

    public ViewModel(ObservableCollection<Item> items)
    {
        _source = items;            
        _clone = new ObservableCollection<Item>(items);
        _clone.CollectionChanged += clone_CollectionChanged;
    }

    void clone_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
                foreach (var newItem in e.NewItems.Cast<Item>())
                {
                    _changes.Add(newItem);
                }
                break;
        }
    }

    public ObservableCollection<Item> Clone
    {
        get { return _clone; }
    }

    private DelegateCommand _commitChangesCommand;

    public ICommand CommitChangesCommand
    {
        get { return _commitChangesCommand ?? (_commitChangesCommand = new DelegateCommand(CommitChanges, CanCommitChanges)); }
    }

    private void CommitChanges(object sender)
    {
        foreach (var change in _changes)
        {
            _source.Add(change);
        }

        _changes.Clear();
    }

    private bool CanCommitChanges(object sender)
    {
        return _changes.Any();
    }
}

public class Item
{
}

public class DelegateCommand : ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;

    public event EventHandler CanExecuteChanged;

    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public DelegateCommand(Action<object> execute,
        Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null || _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    public void RaiseCanExecuteChanged()
    {
        if (CanExecuteChanged != null)
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }
}
}

Upvotes: 2

Related Questions