【问题标题】:WPF Datagrid Combobox binding to each other within the datagridWPF Datagrid Combobox在datagrid内相互绑定
【发布时间】:2011-02-28 18:34:09
【问题描述】:

如何让 WPF 数据网格组合框绑定到数据网格元素中。

例如:我有一个 People => 姓名、年龄和性别的集合。

  • Foo,18 岁,男
  • 嘘,21,男
  • FooBoo,30,女

现在在数据网格中,我有一个组合框 (DataGridComboBoxColumn),其中仅包含名称 (Foo、Boo、FooBoo) ItemsSoure 和两个 DataGridTextBoxColumn。 Datagrid 绑定到 ObservableCollection 或 DataTable。现在当用户选择一个名字时。年龄和性别在两个DataGridTextBoxColumn 中显示(绑定)并添加到DataTable 中。

谢谢。

【问题讨论】:

    标签: c# wpf datagrid combobox


    【解决方案1】:

    首先,您的ComboBox 需要定义如下:

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

    连同它需要的任何其他东西(以及ItemsSource 的正确绑定)。这样,它“包含”整个 Person 类,但只显示名称。
    现在,你可以定义你的TextBoxes 像

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

    不过,就将它添加到 DataTable 源而言,我不明白您想要什么。

    此外,如果使用 MVVM 模式,这可能会容易得多,其中您有一个具有“SelectedPerson”属性的 ViewModel 类。

    【讨论】:

    • 感谢您的回答。但我希望与您在 Datagrid 中而不是在窗口中所说的内容相同。
    猜你喜欢
    • 2011-05-06
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2021-07-07
    • 1970-01-01
    • 2016-04-30
    相关资源
    最近更新 更多