I asked a question previous,
Showing just the top line in a combobox. That question was answered, but now I've got a new one which is directly related to it. In that previous question Li Wang answered with some code examples, which I've tried to implement, but I'm getting an error
that says, "Items collection must be empty before using ItemsSource". I've not encountered this before nor seen it elsewhere. And at least as far as I understand how it works, I don't see why I'm getting that. I thought that the ItemsSource of my
ComboBox was empty before I filled it. Here is the XAML for the combobox:
<ComboBox x:Name="cmbProducts"
ItemsSource="{Binding ProductsList}"
IsEditable="False"
Grid.Row="8"
Grid.Column="1"
Margin="6,0,0,3"
SelectedValue="{Binding OrderLine.Product_ID}"><common:ComboBoxItemTemplateSelector><common:ComboBoxItemTemplateSelector.SelectedTemplate><DataTemplate><TextBlock Text="{Binding Product_Name}" /></DataTemplate></common:ComboBoxItemTemplateSelector.SelectedTemplate><common:ComboBoxItemTemplateSelector.DropDownTemplate><DataTemplate><DockPanel LastChildFill="True"
Width="500"><TextBlock Text="{Binding Product_Name}"
DockPanel.Dock="Top"
FontSize="15"
Padding="5"
Background="#2288C6"
Foreground="White"
TextWrapping="Wrap"
Width="500"
HorizontalAlignment="Left" /><TextBlock Text="{Binding Description}"
TextWrapping="Wrap"
Padding="3"
DockPanel.Dock="Top"
FontWeight="Bold"
HorizontalAlignment="Left" /><Grid Margin="3,0"
HorizontalAlignment="Left"><Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition /><ColumnDefinition Width="10" /><!-- used for separation between the "first" column and the "second" column --><ColumnDefinition /><ColumnDefinition /></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition /><RowDefinition /></Grid.RowDefinitions><TextBlock HorizontalAlignment="Left">Supplier:</TextBlock><TextBlock Text="{Binding Last_Supplier}"
HorizontalAlignment="Left"
TextWrapping="Wrap"
FontWeight="Bold"
Margin="3,0,0,0"
Grid.Column="1" /><TextBlock Grid.Column="3"
Margin="3,0,0,0"
HorizontalAlignment="Left">SKU:</TextBlock><TextBlock Text="{Binding SKU}"
Grid.Column="4"
HorizontalAlignment="Left"
Margin="3,0,0,0"
FontWeight="Bold" /><TextBlock Grid.Row="1"
HorizontalAlignment="Left">Cost:</TextBlock><TextBlock Text="{Binding Current_Price, StringFormat=C}"
HorizontalAlignment="Left"
Grid.Row="1"
Grid.Column="1"
Margin="3,0,0,0"
FontWeight="Bold" /><TextBlock Grid.Row="1"
Grid.Column="3"
Margin="3,0,0,0"
HorizontalAlignment="Left">Unit:</TextBlock><TextBlock Text="{Binding Sales_Unit}"
HorizontalAlignment="Left"
Grid.Row="1"
Grid.Column="4"
Margin="3,0,0,0"
FontWeight="Bold" /></Grid></DockPanel></DataTemplate></common:ComboBoxItemTemplateSelector.DropDownTemplate></common:ComboBoxItemTemplateSelector></ComboBox>
For what it's worth, here's what I've got for the ProductsList in my VM:
//defined at the class level
public List<Product> ProductsList { get; set; }
//this method is called from the VM's constructors (I've got 2 constructors)
private void GetAllProducts()
{
ProductsList = App.MainDataContext.Products.OrderBy(p => p.Product_Name).ToList();
}
Now finally here's the error message I'm getting:
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Items collection must be empty before using ItemsSource.
Source=PresentationFramework
StackTrace:
at System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value, Func`2 GetSourceItem)
at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.Activate(Object item)
at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Run(Object arg)
at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.UIElement.UpdateLayout()
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at System.Windows.Window.SetRootVisual()
at System.Windows.Window.SetRootVisualAndUpdateSTC()
at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
at System.Windows.Window.CreateSourceWindowDuringShow()
at System.Windows.Window.SafeCreateWindowDuringShow()
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at CoreFramework.UserControls.CoreHomeView.ReceiveOpenViewMessage(OpenViewMessage message) in D:\Src\CoreFramework\CoreFramework\UserControls\CoreHomeView.xaml.cs:line 56
InnerException:
Rod