Data Binding a DataGridComboBoxColumn

Topics: Getting Started
Jul 15, 2013 at 2:10 PM
Edited Jul 15, 2013 at 2:11 PM
I'm trying to populate a DataGridComboBox Column from a List<header> property from the according ViewModel.
<DataGrid Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="outlets" CanUserAddRows="True" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Width="*" Binding="{Binding name}" Header="Name" HeaderStringFormat="Name" SortDirection="Ascending" />
                    <DataGridComboBoxColumn ItemsSource="{Binding Path=headers}" SelectedValueBinding="{Binding outletheader}"
                        Width="*" Header="Header" HeaderStringFormat="Name" SortDirection="Ascending"                        
                    />

                    <DataGridCheckBoxColumn Header="Visible" Width="60" Binding="{Binding visible}" ClipboardContentBinding="{x:Null}"/>
                </DataGrid.Columns>
            </DataGrid>
The selected value binding does not raise an error so I guess it works - but how do I correctly reference to the list<header> as itemsource? (ItemsSource="{Binding Path=headers}" ) is obviously not the way to go

thank you
Jul 16, 2013 at 10:00 AM
Solved it with:
 <DataGridComboBoxColumn Width="*">
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.headers}"/>
                                <Setter Property="DisplayMemberPath" Value="shortdescription" />
                                <Setter Property="SelectedValuePath" Value="headerID" />
                                <Setter Property="SelectedItem" Value="{Binding outletheader}" />                                
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.headers}"/>
                                <Setter Property="DisplayMemberPath" Value="shortdescription" />
                                <Setter Property="SelectedValuePath" Value="headerID" />
                                <Setter Property="SelectedItem" Value="{Binding outletheader}" />
                        </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>