0

I have a ComboBox defined in a DataGrid as shown. The ItemsSource of the ComboBox is a List of the custom class CodePickList containing Name and ID properties. The DisplayMemberPath is the Name property and the SelectedValuePath is the ID property. The SelectedValue is bound to the GLCode property on the DataGrid ItemsSource. (Note that the ItemsSource of the DataGrid is a view.) The ComboBox populates properly and the binding of the ComboBox selection to the GLCode property works as well. All fine and good for new data. The problem is with the existing data in the ItemsSource of the DataGrid. The textblock of the ComboBox is simply blank. Apparently the ComboBox is unable to match the GLCode property on the DataGrid ItemsSource with the ID property on the ComboBox ItemsSource when the ComboBox ItemsSource loads. These properties are of the same type. WinForms handles this scenario but I can't get it to work in WPF.

<DataGrid ItemsSource="{Binding EntityDetailsView.View}">
    <DataGrid.Columns>
        <DataGridTemplateColumn>
             <DataGridTemplateColumn.CellTemplate>
                 <DataTemplate>
                        <ComboBox ItemsSource="{BindingPath=DataContext.CodePickList, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                  DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValue="{Binding GLCode, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                                  IsEditable="False" IsReadOnly="False"/>
                 </DataTemplate> 
             </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • No need to make a celltemplate like this, you can use a DataGridComboBoxColum, see this post for reference http://stackoverflow.com/questions/5409259/binding-itemssource-of-a-comboboxcolumn-in-wpf-datagrid – Hannish Mar 31 '13 at 19:18
  • @Hannish, Thank you, I have seen the post you refer to. You are correct about the CellTemplate but the display problem persists. Other posts lead me to believe that this a timing issue, meaning that the SelectValue is somehow evaluated against the ComboBox too soon or too late. No luck in figuring it out so far. – user2227274 Apr 03 '13 at 05:11

1 Answers1

0

It turns out that the GLCode in the ItemsSource for the DataGrid is coming from the Database (through Entity Framework) as a fixed-length string (padded-right). The ID in the ItemsSource of the ComboBox was a normal C# string. Therefore, the values did not match and the existing data would not display. The solution was to pad the ID strings in the ComboBox ItemsSource. The ComboBox now works as expected.