MVVM, two synchronized Grid Data

447 views Asked by At

I want to do a control for adding and removing items from a two list (Selected and UnSelected), like this:

Screenshot of control

But I can't find a good way to do this; how can I use the GridData (or similar control like GridControl of Devexpress) for binding two List and modify it?

Problems:

  • I can't use ObservableCollection with this control
  • I can't bind the SelectedItems

If you have any suggestion or sample for some work, it will be a great help

1

There are 1 answers

7
Snowbear On BEST ANSWER

Can you use two observable collections? One for selected and one for unselected. It seems to be the easiest way to implement such functionality.

public class MainViewModel 
{
    private readonly ObservableCollection<Item> _selectedItems = new ObservableCollection();
    private readonly ObservableCollection<Item> _unselectedItems = new ObservableCollection();

    public IEnumerable<Item> SelectedItems { get { return _selectedItems; } }
    public IEnumerable<Item> UnselectedItems { get { return _unselectedItems; } }

    private void UnselectItems()
    {
        MoveFromOneCollectionToAnother(_unselectedItems, _selectedItems, ...);
    }

    private void SelectItems()
    {
        MoveFromOneCollectionToAnother(_selectedItems, _unselectedItems, ...);
    }

    private void MoveFromOneCollectionToAnother(ICollection<Item> source, ICollection<Item> destination, IEnumerable<Item> itemsToMove)
    {
        foreach (var item in itemsToMove)
        {
            source.Remove(item);
            destination.Add(item);
        }
    }
}