I have a WPF form with a DataGrid. This DG contains a DataGridTemplateColumn that contains a ComboBox. When I click on the new row of the DG and selected a value from the Combobox and then tab to the next column, the selected value does not stay visible in the combobox column.
However, when I tab back, the correct value shows in the combobox.
How do I keep the selected value showing in the combobox column when I tab off the column?
Here is my comboxbox column xaml:
<DataGridTemplateColumn Header="Type" Width="160">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LocationType.Description, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ComboBox Name="cboAddrtype"
ItemTemplate="{StaticResource dtAddrType}" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}, AncestorLevel=1}, Path=DataContext.LocationTypesObject, Mode=OneTime}"
SelectedItem="{Binding Path=SelectedLocationType, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
SelectedValue="{Binding Path=LocationTypeKey, Mode=TwoWay}"
SelectedValuePath="InternalKey"
Width="100" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
In
CellTemplateyou’re bindingLocationType.Descriptionand inCellEditingTemplateyou’re bindingSelectedLocationTypeandLocationTypeKey. I’m not sure that your binding fromComboBoxwon’t somehow affectLocationType.Description.Furthermore,
SelectedLocationTypeandLocationTypeKeyare not in the context ofDataTemplate. You should add aSourcebinding property to get your ViewModel.