2
votes

My problem is that I can't seem to time the selection changed event from the ComboBox edit, to the TextBlock update. Everything displays property but when I'm getting the Grade in the SelectionChanged event in the code behind, it's not updated with the actual value. To get the actual value I have to change the selection on the ComboBox then click off the line of the datagrid I'm editing. Then when I choose a new grade it is updated to show what the last grade was selected. So even though it seems it should all be connected, the row does not actually update until I click off it, as opposed to when I make a different ComboBox selection. So is there any way to change the way a datagrid updates the data to keep in sync with the ComboBox? Any help is appreciated. Thanks.

<DataGridTemplateColumn Header="Grade" CellStyle="{StaticResource StarNumberCellStyle}">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Grade}" Margin="3,1,3,1">
            </TextBlock>
        </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
     <DataGridTemplateColumn.CellEditingTemplate>
         <DataTemplate>
             <ComboBox ItemsSource="{Binding DataContext.Grades, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" 
                 SelectedItem="{Binding Grade,Mode=TwoWay, NotifyOnTargetUpdated=True}" Tag="{Binding}" ComboBox.IsTextSearchEnabled = "True">
                 <i:Interaction.Triggers>
                     <i:EventTrigger EventName="SelectionChanged">
                         <i:InvokeCommandAction Command="{Binding DataContext.GradeSelectCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" 
                               CommandParameter ="{Binding}"/>
                     </i:EventTrigger>
                 </i:Interaction.Triggers>
             </ComboBox>
         </DataTemplate>
     </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
1

1 Answers

1
votes

See my answer about DataGridTemplateColumn caveats here: DataGrid and Observable Collection in WPF

In short, this should work for you (UpdateSourceTrigger=PropertyChanged):

<ComboBox ... 
          SelectedItem="{Binding Grade, UpdateSourceTrigger=PropertyChanged, 
                                 Mode=TwoWay, NotifyOnTargetUpdated=True}"
          ... >