【问题标题】:DataGridComboBoxColumn set ItemSource based on selected item of another DataGridComboBoxColumnDataGridComboBoxColumn 根据另一个 DataGridComboBoxColumn 的选定项设置 ItemSource
【发布时间】:2020-05-05 19:52:47
【问题描述】:

我的数据网格 ClassificationComboBox 和 DisclosureNoteComboBox 中有 2 个 DataGridComboBoxColumn。 第一个工作正常,我看到它填充了数据。 我想要的是,当在第一个项目中选择一个项目时,即 ClassificationComboBox ,我希望第二个项目显示第一个项目中所选项目的数据列表。 ClassificationComboBox 中选定的项目是分类类型,它有一个名为 ClassificationRecords 的列表,我希望将其填充到 DisclosureNoteComboBox 中。

这是我的 XML 的 sn-p。

              <materialDesign:DataGridComboBoxColumn Header="Classification" IsEditable="False" x:Name="ClassificationComboBox"
                                                       ItemsSourceBinding="{Binding ElementName=TrialBalanceViewName, Path=Report.Classifications}"
                                                       DisplayMemberPath="Name"
                                                       SelectedValuePath="Id" 
                                                       SelectedValueBinding="{Binding ClassificationRecord.ClassificationId}"
                                                       />
                <materialDesign:DataGridComboBoxColumn Header="Disclosure Note" IsEditable="False" x:Name="DisclosureNoteComboBox"
                                                       ItemsSourceBinding="{Binding ElementName=ClassificationComboBox, Path=SelectedValueBinding.ClassificationRecords}"
                                                       SelectedValuePath="DisclosureNote"
                                                       SelectedValueBinding="{Binding ClassificationRecord.DisclosureNote}"
                                                       />

我怀疑我的问题是如何为第二个(尤其是路径)构造 ItemsSourceBinding。我认为这是错误的,但我不确定如何调用第一个的选定项目并在其上调用 ClassificationRecords 作为第二个的 ItemSource

ItemsSourceBinding="{Binding ElementName=ClassificationComboBox, Path=SelectedValueBinding.ClassificationRecords}"

【问题讨论】:

    标签: c# wpf data-binding datagrid datagridcomboboxcolumn


    【解决方案1】:

    您不能这样处理问题,因为数据网格列定义不包含您想要的数据。它们是对 DataGrid 如何显示其项目的说明,而不是项目本身的容器。

    要解决这个问题,您需要绑定到数据模型,而不是列定义。

    • A 列:绑定到属性foo
    • B 列:绑定到属性bar 使用由属性 foo 填充的 ItemsSource。

    【讨论】:

    • 感谢您的回复。这解决了我的问题。我所做的是在每一行上创建一个属性,该属性根据第一个组合框的值公开一个列表,该列表用作第二个组合框的项目源。
    猜你喜欢
    • 2021-11-04
    • 2016-12-30
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 2011-10-26
    • 2013-04-24
    相关资源
    最近更新 更多