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?