0
votes

Suppose I have one ObservableCollection that I insert, update and delete from.

public ObservableCollection<Item> AllItems {get;set;} 

The Item class (which implements INotifyPropertyChanged) looks like this:

public class Item
{
  public int Id {get;set;}
  public int Kind {get;set;}
  public string Text {get;set;}
}

I have this bound in my View in one ListView. Everything works, meaning if I delete or add or updates Items in the ObservableCollection from my ViewModel, they are updating in my ListView as expected.

But now I need to split the ListView in 2 ListViews in my View. The first ListView must display all items where Item.Kind=1. And the other ListView must have all items where Item.Kind=2.

I don't mind binding to two different ObservableCollections from my ViewModel, but I would really like a solution where I can just do:

var item = new Item { Id=22, Kind=1, Text="SomeText" };
AllItems.Add(item);
AllItems.First(x => x.Id==22).Text = "SomeOtherText";
AllItems.Remove(item);

And then these operations are automatically reflected in the 2 ListViews. So in the above example, the new Item is only viewed, updated and removed from the first ListView (where Item.Kind=1).

Is that possible?

EDIT: I should say that I have tried to use two CollectionViewSources with the same ObservableCollection as source. But that fails and does not seem to be the correct solution.

1

1 Answers

3
votes

create two ListCollectionViews with different filters - one for each ListView

public ICollectionView Items_1 { get; private set; }
public ICollectionView Items_2 { get; private set; }

public ViewModel
{
    Items_1 = new ListCollectionView(AllItems);
    Items_1.Filter = o => (o as Item).Kind == 1;

    Items_2 = new ListCollectionView(AllItems);
    Items_2.Filter = o => (o as Item).Kind == 2;
}