1

We're using entity framework to retrieve our data. We're trying to bind a collection view source to a combo box to display the data. Here's the definition for the CollectionViewSource:

<CollectionViewSource x:Key="aSICodesControlledEnvironmentViewSource" d:DesignSource="{d:DesignInstance {x:Type AsiEF:ASICodesControlledEnvironment}, CreateList=True}">

AsiEF is the entity framework assembly. Here's the XAML for the combo box:

<ComboBox x:Name="cmbControlledEnvLast30" Margin="480,20,0,0" DisplayMemberPath="ContEnvDesc"  SelectedValue="ContEnvDesc"  Width="150"  FontSize="14" 
      ItemsSource="{Binding Source={StaticResource aSICodesControlledEnvironmentViewSource}}">
<CollectionViewSource>
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="DisplayOrder" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</ComboBox>

As you can see, we're trying to sort the data by the field DisplayOrder, even though that field isn't visible in the combo box, I would still think that the CollectionViewSource should be able to sort the data by that field.

But where this falls down is in trying to retrieve the data and assign it to the Source of the collection view source in the user control's loaded event:

ComboBoxSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("aSICodesControlledEnvironmentViewSource")));
ComboBoxSource.Source = asiContext.ASICodesControlledEnvironments;

It hangs on the second line, when attempting to assign Source property of the CollectionViewSource object ComboBoxSource. The asiContext is our AsiEF's ObjectContext. The error that gets thrown is, "Items collection must be empty before using ItemsSource". I'm sorry, I don't get what we're doing wrong. I've tried removing the assigning in the ComboBox of ItemsSource from the XAML, but that doesn't work. So, where are we going wrong?

Rod
  • 4,107
  • 12
  • 57
  • 81

1 Answers1

2

I think you should declare the SortDescriptions in CollectionViewSource definition

<CollectionViewSource x:Key="aSICodesControlledEnvironmentViewSource"
                      d:DesignSource="{d:DesignInstance {x:Type AsiEF:ASICodesControlledEnvironment}, CreateList=True}">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="DisplayOrder" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

and remove the additional CollectionViewSource from ComboBox. This will be interpreted as ComboBox.Items and later when ComboBox.ItemsSource is binding it throws the exception.

<ComboBox x:Name="cmbControlledEnvLast30"
          ItemsSource="{Binding Source={StaticResource aSICodesControlledEnvironmentViewSource}}">

</ComboBox>
LPL
  • 16,827
  • 6
  • 51
  • 95
  • I've just done as you suggested, but got the error message, "'System.Windows.Data.BindingListCollectionView' view does not support sorting." Got that error on the assignment of ComboBoxSource's Source property in Loaded event. – Rod May 17 '13 at 17:14
  • Sorry. I've no idea about this because BindingListCollectionView does support sorting. See [here](http://msdn.microsoft.com/en-us/library/system.windows.data.bindinglistcollectionview.sortdescriptions.aspx). – LPL May 17 '13 at 18:44
  • Thank you for the link, LPL. I see what you mean, BindingListCollectionView does support sorting. I'm reaching here, but could it be because the data source is coming from EF? Is the only way to sort a collection that's from EF is to use a LINQ expression? We'd rather use XAML. – Rod May 17 '13 at 19:19
  • Maybe this link can help you: [Entity Framework 4.0 Databinding with sorting not working](http://stackoverflow.com/q/2702434/620360). – LPL May 17 '13 at 20:56
  • Thank you, LPL, I think that does address it. – Rod May 18 '13 at 22:04