Why the text of combobox show the items with property name?

c# entity-framework-6 linq wpf

Question

I'm working on a WPF project. Items from my database are automatically inserted into a ComboBox that I have. I'm attempting to get the ID of the item that is now chosen in the combo box. The items are displaying, but the property name is also shown in the combo box's text. I used the code to populate the combo box.

using (var Context = new ProfetusModel())
{
    var GetQuestions = Context.Questions.Select(ques => new {Questions=ques.Question1,ID=ques.IdQues }).ToList();
    // CompoQues.ItemsSource = GetQuestions;
    CompoQues.ItemsSource = GetQuestions
    CompoQues.DisplayMemberPath = "Questions";
    CompoQues.SelectedValuePath = "ID";
    CompoQues.SelectedIndex = 0;
}

it's functioning but displays like this picture

combo box image

How can I structure the question that appears in the combo box such that just the content of the question, without the preceding brace, is displayed?

"Questions = What is the name of (text of chosen question)..." is written in the text.

What is the name of (text of chosen question) should be the only thing that appears in the text.

1
0
7/30/2018 2:41:45 PM

Accepted Answer

Update your code by trying to:

public struct ComboItem
{
    public string Question { get; set; }
    public int ID { get; set; }
}

using (var Context = new ProfetusModel())
{
    var GetQuestions = Context.Questions.Select(ques => new ComboItem { Question = ques.Question1, ID = ques.IdQues }).ToList();

    CompoQues.ItemsSource = GetQuestions
    CompoQues.DisplayMemberPath = "Question";
    CompoQues.SelectedValuePath = "ID";
    CompoQues.SelectedIndex = 0;
}
1
7/31/2018 2:10:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow