Q:

Display a field of datagrid instead of the whole column

I have a datagrid control inside a combobox like this :
 <ComboBox    DisplayMemberPath="{Binding CodeGDP_Collection/gdp_code}"  >
                        <ComboBoxItem >
                            <DataGrid  ItemsSource="{Binding CodeGDP_Collection}"   AutoGenerateColumns="False">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding gdp_code}" />
                                    <DataGridTextColumn Binding="{Binding gdp_nom}" />
                                    <DataGridTextColumn Binding="{x:Null}"/>
                                    <DataGridTextColumn Binding="{Binding gdp_ville}" />
                                    <DataGridTextColumn Binding="{Binding gdp_code_postal}"/>
                                </DataGrid.Columns>
                            </DataGrid>
                        </ComboBoxItem>
                    </ComboBox>

I'd like that when I select a line in the datagrid , only one field will be displayed in the combobox (for example gdp_code).
In the current case, The whole column of gdp_code is displayed in each selection.
So How can I fix it??

A:

For binding selectedItem you will have to provide itemsSource to your combobox, which is not beneficial in this case.
You can simple bing combobox.text to your value and combobox will display it even if it is not in the list.
You can bind the selected item from grid to a property and then set combobox text to the selected property value.
I did this some time ago, and below  code is not written in a designer hence it can be errorous but it is to give you an idea.
<ComboBox 
DisplayMemberPath="DataContext.MySelectedItem.gdp_code" 
Text="{Binding MySelectedItem.gdp_code}" 
IsEditable="True">
<ComboBox.Items>
    <ComboBoxItem>
        <ComboBoxItem.Template>
            <ControlTemplate>
                <DataGrid SelectedItem={Binding MySelectedItem} ItemsSource="{Binding CodeGDP_Collection}"   AutoGenerateColumns="False">
                    <DataGrid.Columns>
                       <DataGridTextColumn Binding="{Binding gdp_code}" />
                       <DataGridTextColumn Binding="{Binding gdp_nom}" />
                       <DataGridTextColumn Binding="{x:Null}"/>
                       <DataGridTextColumn Binding="{Binding gdp_ville}" />
                       <DataGridTextColumn Binding="{Binding gdp_code_postal}"/>
                    </DataGrid.Columns>
                </DataGrid>
            </ControlTemplate>
        </ComboBoxItem.Template>
    </ComboBoxItem>
</ComboBox.Items>

