Binding to ObservableCollection works, but not ListCollectionView

622 Views Asked by At

I'm trying to get my ListCollectionView to bind to a combo box. However, it seems to only work when I bind to my ObservableCollection.

Properties:

private ListCollectionView sitesView;
public ListCollectionView SitesView
{
        get { return sitesView; }
        set
        {
            sitesView = value;
            RaisePropertyChanged(() => SitesView);
        }
}
public ObservableCollection<ISite> SitesCollection { get; set; }

Constructor:

SitesCollection = new ObservableCollection<ISite>();      
SitesView = (ListCollectionView)CollectionViewSource.GetDefaultView(SitesCollection);

When binding like so:

<ComboBox Grid.Row="2" ItemsSource="{Binding SitesView, Mode=TwoWay}"/>

any item I add to SitesCollection does not get shown when I click the drop down in my combo box. But if I do the binding like so:

<ComboBox Grid.Row="2" ItemsSource="{Binding SitesCollection, Mode=TwoWay}"/>

it works fine and I see the items when I click the drop down.

Attempts at fixing: After I add the an item to the SitesCollection, I tried to raise property change notifications on both the ListCollectionView and the ObservableCollection and it didn't make a difference. Any ideas?

1

There are 1 best solutions below

0
Adam Vincent On

I didn't have time to test my answer; I apologize and will follow up.

I think your issue is because CollectionViewSource.GetDefaultView(...) returns an ICollectionView, which I believe is essentially just a wrapper that enables the underlying data to be sorted.

Try setting SitesView to the SourceCollection of the DefaultView.

SitesView = (ListCollectionView)CollectionViewSource.GetDefaultView(SitesCollection).SourceCollection