【问题标题】:DataGridComboBoxColumn ItemSource Binding Doesn't workDataGridComboBoxColumn ItemSource 绑定不起作用
【发布时间】:2010-09-07 15:15:05
【问题描述】:

在来自资源的 DataGridComboBoxColumn ItemSource 的所有示例中。 它不能直接绑定到 CodeBehind 中的列表吗?

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    这取决于您将直接绑定到 CodeBehind 中的列表的意思。

    您可以使用...声明该列

    <DataGridComboBoxColumn x:Name="m_column" ../>
    

    然后在代码隐藏中设置 ItemsSource...

    m_column.ItemsSource=yourItemsSource
    

    但是您不能直接在 XAML 中使用绑定,例如:

    <DataGridComboBoxColumn ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=YourElement}}"/>
    

    因为DataGridComboBoxColumn 不是可视化树的一部分。

    有解决方法。最简单的是使用 DataGridTemplateColumn 并将 ComboBoxes 直接放在 edit-DataTemplate 中。如果您使用 ViewModel,则可以通过它提供数据。否则,请查看 herehere 以了解解决方法。

    【讨论】:

    • 我在 Joe Morrison 的博客(上面的链接)中发现扩展 DataGridComboBoxColumn 方法对于绑定视图模型特别有用。
    猜你喜欢
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2021-09-17
    • 2020-05-05
    • 2011-03-16
    • 1970-01-01
    相关资源
    最近更新 更多