Question

I want to populate a combo box with items from my database i did this:

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

        }
    }
}
}

which seems like its partially working, i am able to hover over the items in combo box, and they get highlighted, but they don't have the text. What is the reason for this? Any help would be greatly appreciated. I am using 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">
Was it helpful?

Solution

By adding

ItemsSource={Binding}

you tell the ComboBox to use its DataContext as ItemsSource.

Remove this and try again. Further i recommend to read on data binding.

Edit: Also remove

DisplayMemberPath='Title'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top