簡體   English   中英

綁定wpf組合框時,在使用ItemsSource之前,Items集合必須為空

[英]Items collection must be empty before using ItemsSource when binding wpf combobox

將組合框與列表綁定時,出現問題“在使用ItemsSource之前,Items集合必須為空”。 我們該如何解決呢?

我的代碼如下

<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>

我的用戶類是

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

打電話時

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

發生錯誤,我該如何解決?

移動或刪除該<popup>元素。

現在,默認情況下,它構成了Items集合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM