1

I want to bind something to a combobox that will display something different than it's value. What would be the suggested/most common data container to link the combobox to? Would it be a custom type and then do a list of that type? A data table? I am using vb.net and wpf. The list would be something like:

dog,1 cat,2 bird,3 fish,4

The combobox would display the animal name and the value would be the number. The data to populate the combobox will come from a MYSql database.

volderArt
  • 127
  • 1
  • 11

2 Answers2

0

Set the DisplayMemberPath on your Combobox to the property of the underlying object you want to display.

So assuming you have an object like this:

Public Class Animal
{
string Name {get;set;}
int Number {get;set;}
}

You would set your DisplayMemberPath to Name.

Else check out this link for a more complete answer: Binding WPF ComboBox to a Custom List

Community
  • 1
  • 1
Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
  • Thank you for the quick response. So you are saying that it is recommended that I create a custom class and then bind the combobox to the list(of animal) rather than bind to a datatable or something else? – volderArt Jul 08 '13 at 18:43
  • @volderArt it really depends on your data model and the technology you are using. There are many different alternatives from DataSet, to ObservableCollections to your own customized classes. I would suggest you to walk through this link: http://msdn.microsoft.com/en-us/library/ms752347.aspx – Fabian Bigler Jul 08 '13 at 18:50
  • I think sometimes a question is asked and then after some discussion it is realized that a different question needs to be asked. I suppose that I am asking how I figure out what datatype I would choose with the end goal of binding it to a combobox. What kind of questions need to be asked to figure out whether I want a datatable or a list(of T) or enum all of which can be bound to a combobox? Certainly one is more common or recommended for this simple task. – volderArt Jul 08 '13 at 18:55
  • @volderArt It all comes down to the requirements of your software and how you think your product will develop. How are your data treated? Do you only display the data or also edit them? Based on questions like these you will make a decision. Once the requirements change, you will have to change your approach accordingly. You can never know from scratch what is the best choice. – Fabian Bigler Jul 08 '13 at 19:00
  • Okay. So, the data is only displayed, not edited. Only a display value and a selected value. What would you choose? what other questions would you ask? – volderArt Jul 08 '13 at 19:15
  • @volderArt If there the items in your combobox which always stay the same, I would create an enum (If the data only consists of DisplayText and Number). If they can change or if the data can expand (e.g. more attributes), fetch your data somehow and present them either via datatable or the custom object in my answer, whatever you prefer. – Fabian Bigler Jul 08 '13 at 19:40
0

You can populate with anything you want, you can also display multiple items like so

<ComboBox ItemsSource{Binding ItemList}>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding AnimalName}" />
<Checkbox IsChecked={Binding SelectAnimal}" Content="{Binding Age}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

The Select animal is just so you can get an idea of doing a template

Jonah Kunz
  • 670
  • 8
  • 19