0

I am trying to bind a list of strings to my combo box. But when I add a new item to the list or read items from the file, I receive Exception that Items collection must be empty before using ItemsSource. Remember that I need to put a button at the end of the Combobox. what can be the issue?

my Xaml code

 <ComboBox Grid.Column="1" Text="Category">
                <ComboBox.ItemsSource>
                    <CompositeCollection>
                        <CollectionContainer Collection="{Binding Categories}" />
                   <ComboBoxItem>
                    <Button Style="{StaticResource styleTextButton}"
                                    Content="New Category"
                                    Command="{Binding AddNewCategoryCommand}" 
                                    Width="235"/>
                </ComboBoxItem>
                    </CompositeCollection>
                </ComboBox.ItemsSource>
            </ComboBox>

I have read “Items collection must be empty before using ItemsSource.” but it did not help me to solve the problem.

ad79h
  • 53
  • 4

1 Answers1

0

You cannot add an item directly to the Items property after you have set the ItemsSource property.

If you use an ItemsSource, you should add a new item to the source collection, i.e. Categories in this case.

Either you use an ItemsSource or you don't. It's one way or the other but not both.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • The XAML code for what? How are you currently adding an item when you get the exception? – mm8 Aug 14 '20 at 11:00