MVVM, two synchronized Grid Data
I want to do a control for adding and removing items from a two list (Selected and UnSelected), like this:
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 bindi开发者_如何学运维ng 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
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);
}
}
}
精彩评论