![](/img/trans.png)
[英]Got error 'Items collection must be empty before using ItemsSource' when using a ComboBox in a WPF Application
[英]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.