1
votes

when binding the combobox with a list, get the issue "Items collection must be empty before using ItemsSource." how can we resolve this?

My code is below

<ComboBox x:Name="ComboBoxUsers" DisplayMemberPath="Name" SelectedValuePath="Id" ItemsSource="{Binding}" VerticalContentAlignment="Center" HorizontalAlignment="Left" Margin="113,14,0,0" VerticalAlignment="Top" Height="32"  Width="280" FontSize="14" Cursor="Hand" SelectionChanged="ComboBoxUsers_SelectionChanged">
            <ComboBox.Background>
                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#FFF3F3F3" Offset="0"/>
                    <GradientStop Color="#FFEBEBEB" Offset="0.31"/>
                    <GradientStop Color="#FF6C6464" Offset="1"/>
                </LinearGradientBrush>
            </ComboBox.Background>
            <Popup AllowsTransparency="false" Focusable="false"/>
        </ComboBox>

My User class is

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

when calling

Users = GetUsers();
        ComboBoxUsers.ItemsSource = Users ;

the error occurs, how can I resolve this?

1

1 Answers

2
votes

Move or remove that <popup> element.

It is now, by default, making up your Items collection.