在绑定wpf组合框时使用ItemsSource之前,项集合必须为空

本文关键字:集合 之前 ItemsSource 组合 wpf 绑定 | 更新日期: 2023-09-27 18:09:56

当将组合框与列表绑定时,会出现"Items collection必须为空才能使用ItemsSource."我们如何解决这个问题?

我的代码在 下面
<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>

我的User类是

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

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

出现错误,我该如何解决?

在绑定wpf组合框时使用ItemsSource之前,项集合必须为空

移动或删除<popup>元素

现在,默认情况下,它组成了您的Items集合。