Erez
Erez

Reputation: 6446

DataGridComboBoxColumn ItemSource Binding Doesn't work

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

Upvotes: 0

Views: 3257

Answers (1)

HCL
HCL

Reputation: 36775

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.

Upvotes: 4

Related Questions