WPF Datagrid Combobox binding to each other within the datagrid
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 an开发者_运维问答d sex shows (bind) in the two DataGridTextBoxColumn and is added to the DataTable.
Thank you.
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 TextBox
es 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.
精彩评论