[英]c# adding items to combo box from database
我想用我數據庫中的項目填充一個組合框,我這樣做是:
public partial class addAssoc : Window
{
private libraryDBEntities1 context;
public addAssoc()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
context = new libraryDBEntities1();
System.Windows.Data.CollectionViewSource libraryDBEntities1ViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("libraryDBEntities1ViewSource")));
// Load data by setting the CollectionViewSource.Source property:
// libraryDBEntities1ViewSource.Source = [generic data source]
var q = from b in context.textbooks
select b.Title;
foreach(var item in q){
titleComboBox.Items.Add(item);
}
}
}
}
這似乎部分起作用,我可以將鼠標懸停在組合框中的項目上,並且它們會突出顯示,但沒有文字。 這是什么原因呢? 任何幫助將不勝感激。 我正在使用WPF
<ComboBox x:Name="titleComboBox" Width="120" VerticalAlignment="Center" Grid.Row="0" Margin="3" ItemsSource="{Binding}" Height="Auto" HorizontalAlignment="Left" DisplayMemberPath="Title" Grid.Column="1">
通過增加
ItemsSource={Binding}
您告訴ComboBox將其DataContext用作ItemsSource。
刪除它,然后重試。 此外,我建議閱讀數據綁定 。
編輯:也刪除
DisplayMemberPath='Title'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.