DataGridComboBoxColumn ItemSource Binding Doesn't work

后端 未结 1 744
抹茶落季
抹茶落季 2021-01-28 13:23

In all of the examples of DataGridComboBoxColumn ItemSource comming from the Resources. Couldn\'t it be binding directly to a list in the CodeBehind ?

相关标签:
1条回答
  • 2021-01-28 13:43

    It depends what you mean with binding directly to a list in the CodeBehind.

    You can declare the column with...

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

    and then in code-behind set the ItemsSource...

    m_column.ItemsSource=yourItemsSource
    

    However you can not directly use the binding in XAML, something like:

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

    because DataGridComboBoxColumn is not a part of the visual tree.

    There are workarounds for this. The most simple is using a DataGridTemplateColumn and placing the ComboBoxes directly in the edit-DataTemplate. If you use a ViewModel, you can provide the data through it. Otherwise look here and here for workarounds.

    0 讨论(0)
提交回复
热议问题