Noelle
Noelle

Reputation: 782

Populate a combobox in a datagrid

I have a WPF Datagrid populated with data from one SQL table using Entity Framework, is it possible to populate a combobox in the same Datagrid using data from a different table.

I have this code working

                <DataGridTemplateColumn x:Name="reasonColumn" Header="Reason" Width="Auto">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox>
                            <ComboBoxItem Content="Supplier Quantity"/>
                            <ComboBoxItem Content="Supplier Price"/>
                            <ComboBoxItem Content="Supplier Numbers"/>
                            <ComboBoxItem Content="Supplier Codes"/>
                            <ComboBoxItem Content="Branch Quantity"/>
                            <ComboBoxItem Content="Branch Numbers"/>
                            <ComboBoxItem Content="Branch Codes"/>
                            <ComboBoxItem Content="IM Numbers"/>
                            <ComboBoxItem Content="Pop Prices"/>
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

But I would prefer it to be dynamically populated.

Thanks.

Upvotes: 0

Views: 1178

Answers (1)

Rachel
Rachel

Reputation: 132558

Yes, simply bind your ComboBox.ItemsSource to wherever your collection is

For example, this will bind a collection from your DataGrid's DataContext:

<ComboBox ItemsSource="{Binding DataContext.MyComboBoxList, 
          RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />

As another example, this will bind to a static class containing your collection

<ComboBox ItemsSource="{Binding 
          Source={x:Static local:MyStaticClass.MyComboBoxList}" />

Upvotes: 1

Related Questions