WPF CellTemplate does not refresh the text in the CellTemplate after editing

Aug 11, 2009 at 7:27 PM

I have two tables in SQL: Person and Cars. The "Person" table holds first names and the car the person drives. The "Cars" table holds a list of available cars. I want to list all the persons in the table along with their cars and when users click on the car column, a list of available cars is displayed in a combobox and then the users can choose one from the list. When the box is closed the cell should reflect the car selected. The XAML code for this is below:

<my:DataGrid AutoGenerateColumns="False" Canvas.Left="45" Canvas.Top="130" Height="174" 
 Name="dataGrid1" Width="264" ItemsSource="{Binding Source={StaticResource PersonList}}">
                <my:DataGridTextColumn CanUserReorder="False" Header="First Name" IsReadOnly="True" 
                                       Binding="{Binding Path=FirstName}"/>
                <my:DataGridTemplateColumn Header="Car">
                            <TextBlock Text="{Binding Path=Car}"/>
                            <ComboBox ItemsSource="{Binding Source={StaticResource CarList}}" DisplayMemberPath="Car"/>

This is the latest WPF version that I have downloaded from the site. The problem with this code is that after users pick a car the textblock is not updated, in other words the car that was listed before is listed again even after the users have made a selection in the combobox and closed the box.

Any suggestions?

thank you.

Aug 14, 2009 at 12:32 PM

In your CellEditingTemplate you need to set a two way binding of the SelectedItem of the ComboBox to the Car property.  You also need to set IsSychronizedWithCurrentItem=’false’ on the <ComboBox /> inside the <DataGridTemplateColumn />.