0
<ComboBox Grid.Row="1" Margin="0,0,0,175" DataContext="{Binding }" />

I've set the DataContext of the xaml to a class.

Within in this I have a class called Player. A Player has a property array of another object called Quests. Each Quest has a name property.

How can I bind the content of the combobox to the Quest array, and have each item represented by its name property?

I'm assuming I will have to use binding

Jim
  • 2,974
  • 2
  • 19
  • 29
  • 1
    Possible duplicate of [WPF ComboBox binding ItemsSource](http://stackoverflow.com/questions/28373643/wpf-combobox-binding-itemssource) – Andy Dec 13 '16 at 20:41

2 Answers2

0

Never mind. Found out :D

If anyone else had the same problem:

Assume you have a List called Foos in your window / page. Foo has a property Name. Now you set up the binding in XAML as follows:

<ComboBox ItemsSource="{Binding Path=Foos}"
DisplayMemberPath="Name"
SelectedValuePath="Name"

Referenced from WPF Databinding combobox to a list<string>

Community
  • 1
  • 1
0
  1. the statement DataContext="{Binding }" simply takes the current value of the DataContext property and assigns it back- this will have no effect.

  2. Assuming that the DataContext of this view has a property named Quests, what you need to do is:

    <ComboBox Grid.Row="1" Margin="0,0,0,175" ItemsSource="{Binding Quests}" />
    

Note that ComboBox derives from ItemsControl, and as such- setting the ItemsSource property will always do the trick.

You will probably need to create a data template for the quest item in order to see something of actual interest. This should get you started with Data Templates.

Once you've created a Data Template, set it to the ComboBox using the ItemTemplate property (Once more, a courtesy of ItemsControl).

Eyal Perry
  • 2,255
  • 18
  • 26