views:

85

answers:

1

How to get a WPF datagrid combobox to bind within the datagrid elements.

For example: I have a collection of People => Name, Age and Sex.

  • Foo, 18, Male
  • Boo, 21, Male
  • FooBoo, 30, Female

Now inside the datagrid i have a combobox (DataGridComboBoxColumn) with the collection of the names only (Foo, Boo, FooBoo) ItemsSoure and two DataGridTextBoxColumn. The Datagrid is binded to a ObservableCollection or DataTable. Now when the user select a name. The age and sex shows (bind) in the two DataGridTextBoxColumn and is added to the DataTable.

Thank you.

A: 

First, your ComboBox needs to be defined something like:

<ComboBox Name="PeopleCombo" ItemsSource="{Binding ...}" DisplayMemberPath="Name" />

along with anything else that it needs (and the correct binding for ItemsSource). This way it "contains" the entire Person class but only displays the name.
Now, you can just define your TextBoxes like

<TextBox Text="{Binding ElementName=PeopleCombo, Path=SelectedItem.Sex}" />

I do not understand what you want as far as adding it to the DataTable source, though.

Also, this would probably be a lot easier with an MVVM pattern, where you have a ViewModel class with a "SelectedPerson" property.

JustABill
Thanks for your answer. But I'm looking to have the same as what you have said in a Datagrid not in a window.
aalhamad