DataGridComboBoxColumn binding

Sep 16, 2009 at 5:07 PM

I am having trouble binding a DataGridComboBoxColumn. I have the following markup:

                <my:DataGrid ItemsSource="{Binding Path=ListOfMinorLocations}" AutoGenerateColumns="False" 
                    Name="dgMinorLocation" SelectionMode="Single" SelectionUnit="FullRow" CanUserAddRows="True" CanUserDeleteRows="True">
                    <my:DataGrid.Columns>
                        <my:DataGridTextColumn Header="ID" Width="SizeToCells"  MinWidth="100" Binding="{Binding minor_location_id}" IsReadOnly="True"/>
                        <my:DataGridTextColumn Header="Minor Location" Width="SizeToCells"  MinWidth="350" Binding="{Binding minor_location_name}" />
                        <my:DataGridComboBoxColumn x:Name="cbomajor_location_id" Header="Major Location" MinWidth="200" 
                                                   ItemsSource="{Binding Path=ListOfMajorLocations}" 
                                                   SelectedValueBinding="{Binding Path=major_location_id}" 
                                                   DisplayMemberPath="major_location_name" 
                                                   SelectedValuePath="major_location_id" />
                        <my:DataGridCheckBoxColumn x:Name="Active" Header="Active" Binding="{Binding active}" />
                    </my:DataGrid.Columns>
                </my:DataGrid>
In my view model I have:
            ObservableCollection<major_location> listOfMajorLocations;
            ListOfMajorLocations = new ObservableCollection<major_location>(majorLocationRepository.GetAllMajorLocations());

If I have a standard combobox outside the datagrid, the binding works. It is only when I use a combobox or DataGridComboBox (tried both) 
inside the datagrid that it doesn't work. When I say it doesn't work, I mean the combobox is empty, as if it is not binding. I checked the 
major locations ObservableCollection in debug mode and it is being populated.
Also, the datagrid itself binds all the fields, even the major location ID value (when I put a textbox in place of the combobox).
Thanks.
Sep 16, 2009 at 8:42 PM

I found the answer here:

 http://wpf.codeplex.com/WorkItem/View.aspx?WorkItemId=8153

guess you can't use the datacontext in a DataGridComboBoxColumn

here is what I ended up doing in my markup:

                        <my:DataGridTemplateColumn Header="Major Location" MinWidth="200">
                            <my:DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <ComboBox Name="cbomajor_location_id" 
				                          DisplayMemberPath="major_location_name" 
				                          SelectedValue="{Binding Path=major_location_id}" 
				                          SelectedValuePath="major_location_id" HorizontalAlignment="Stretch" 
                                          ItemsSource="{Binding DataContext.ListOfMajorLocations, RelativeSource={RelativeSource AncestorType={x:Type Window}}, IsAsync=true}" />
                                </DataTemplate>
                            </my:DataGridTemplateColumn.CellEditingTemplate>
                            <my:DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding major_location_id}" />
                                </DataTemplate>
                            </my:DataGridTemplateColumn.CellTemplate>
                        </my:DataGridTemplateColumn>