简体   繁体   English

WPF Datagrid Combobox在数据网格内彼此绑定

[英]WPF Datagrid Combobox binding to each other within the datagrid

How to get a WPF datagrid combobox to bind within the datagrid elements. 如何获取WPF数据网格组合框以绑定到数据网格元素中。

For example: I have a collection of People => Name, Age and Sex. 例如:我有一个人=>名称,年龄和性别的集合。

  • Foo, 18, Male Foo,18岁,男
  • Boo, 21, Male Boo,21岁,男
  • FooBoo, 30, Female FooBoo,30岁,女性

Now inside the datagrid i have a combobox (DataGridComboBoxColumn) with the collection of the names only (Foo, Boo, FooBoo) ItemsSoure and two DataGridTextBoxColumn. 现在,在数据网格中,我有一个组合框(DataGridComboBoxColumn),其组合仅包含名称(Foo,Boo,FooBoo)ItemsSoure和两个DataGridTextBoxColumn。 The Datagrid is binded to a ObservableCollection or DataTable. Datagrid绑定到ObservableCollection或DataTable。 Now when the user select a name. 现在,当用户选择一个名称时。 The age and sex shows (bind) in the two DataGridTextBoxColumn and is added to the DataTable. 年龄和性别在两个DataGridTextBoxColumn中显示(绑定),并添加到DataTable中。

Thank you. 谢谢。

First, your ComboBox needs to be defined something like: 首先,您的ComboBox需要定义为:

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

along with anything else that it needs (and the correct binding for ItemsSource ). 以及它需要的其他任何内容(以及ItemsSource的正确绑定)。 This way it "contains" the entire Person class but only displays the name. 这样,它“包含”整个Person类,但仅显示名称。
Now, you can just define your TextBox es like 现在,您可以像这样定义TextBox

<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. 但是,我不了解您想要将其添加到DataTable源中的什么。

Also, this would probably be a lot easier with an MVVM pattern, where you have a ViewModel class with a "SelectedPerson" property. 同样,使用MVVM模式,这可能会容易得多,在该模式下,您具有具有“ SelectedPerson”属性的ViewModel类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM