0

I have seen a few posts on here with people getting confused on how to bind to a DataGridComboBoxColumn,

I have

<DataGridComboBoxColumn SelectedItemBinding="{Binding Collection}"  DisplayMemberPath="Name" Header="Name" Width="70">

which didnt work..

So I used

<DataGridComboBoxColumn ItemBinding="{Binding Collection}" DisplayMemberPath="Name"> Header="Name" Width="70">

Which again didn't work, why is binding to a datagridcombo different to a original combo box.

<ComboBox ItemsSource="{Binding Collection}" DisplayMemberPath="Name" HorizontalAlignment="Left">

which does work

What is the correct method of binding to a combo box inside a DataGrid?

---Edit---

I might have found the problem, I have a DataGrid binding to a ItemSource, however, I want the ComboBoxColumn to be bounded to a different Itemsource, is this possible?

Cheers

user3428422
  • 4,300
  • 12
  • 55
  • 119

1 Answers1

1

You need to bind to ItemsSource property. Set it in EditingElementStyle.

<DataGridComboBoxColumn>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Collection}"/>
            <Setter Property="DisplayMemberPath" Value="Name"/>
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

In case you want ItemsSource to bind to collection which is outside of DataGrid underlying source object, you can do that as well.

Say you have collection AnotherCollection residing in ViewModel of Window/UserControl, you can bind with it using RelativeSource markup extension.

Also, you have to set SelectedItemBinding to the property where you want to set the value selected from ComboBox and declare same style under ElementStyle of DataGridComboBoxColumn. Suppose property name is Name to which you want to bind.

<DataGridComboBoxColumn SelectedItemBinding="{Binding Name}">
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource"
                    Value="{Binding DataContext.AnotherCollection,
                               RelativeSource={RelativeSource Mode=FindAncestor, 
                                                         AncestorType=Window}}"/>
            <Setter Property="DisplayMemberPath" Value="Name"/>
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource"
                    Value="{Binding DataContext.AnotherCollection,
                              RelativeSource={RelativeSource Mode=FindAncestor, 
                                                         AncestorType=Window}}"/>
            <Setter Property="DisplayMemberPath" Value="Name"/>
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Thanks that seemed to work, however, when I pick a value from the combobox, it disappears when I click off of it, is this the normal behaviour with the code above? – user3428422 Apr 13 '14 at 19:22
  • You need to add `SelectedItemBinding` to the property name to which you want to set the value to and also need to add the same style under ElementStyle. Let me update answer with it. – Rohit Vats Apr 13 '14 at 19:26
  • Thanks, the combo box now has the selected item, but it crashes the grid! (! appears on the row) I guess this is the itemsource complaining and not the XAML code that is wrong? – user3428422 Apr 13 '14 at 19:39
  • When I select a item from the combo box, I then try to click off of it and a ! appears on the side of the row and the row crashes (I cant add/edit etc) – user3428422 Apr 13 '14 at 19:41
  • It seems you some kind of validation in place in your code. Not an issue with the above XAML. – Rohit Vats Apr 13 '14 at 19:42
  • Thanks, I think you're right, will mark as correct, thanks for your help! – user3428422 Apr 13 '14 at 19:44
  • Hi Robit and anyone else who may be interested, I found why the ! kept coming up in the row, it was due not putting in both EditElementStyle and EditingStyle attributes. I am not sure updating the code above is necessary, is up to you. Might help someone! – user3428422 Apr 14 '14 at 06:39
  • Sure I have updated it in case someone needs it in future. Thanks..!! :) – Rohit Vats Apr 14 '14 at 08:08