Why does my DataGridComboBoxColumn clear its value when I leave it?

I have a DataGrid with two columns:

  • DataGridComboBoxColumn
  • DataGridTextColumn.

I set up data validation so that if someone has a value, the other will be erroneous until it has a value. Verification is stupid, but it provides some simple criteria for verification, so I can illustrate this problem.

When I enter something into a text cell, click on the tab, and then go back to the first cell, the first cell shows that it is in an error state (this is correct). The problem is that when I select something from the drop-down list with the list and move away from this cell (either by clicking the tab or by clicking in another cell), the value that I selected for the combo box disappears. I have a binding set to update my source whenever the property changes, so it gets the value that I select as soon as I select it. But, when I move from the cell, the property gets null. I do not see this behavior if the cell is not in an error state.

Can anyone help? Here is the XAML for my DataGrid:

<DataGrid Grid.Row="2" Name="GrdData" ItemsSource="{Binding Path=Dvm.Data}" SelectedItem="{Binding Path=Dvm.SelectedData, Mode=TwoWay}" CanUserAddRows="True" CanUserDeleteRows="False" AutoGenerateColumns="False" Margin="5" SelectionMode="Single" IsEnabled="{Binding Path=IsGridEnabled}"> <DataGrid.Columns> <DataGridComboBoxColumn Header="Column 1" SelectedItemBinding="{Binding Path=Col1, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Width="*" DisplayMemberPath="Description"> <DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=DropDownValues, Mode=OneWay}" /> <Setter Property="IsSynchronizedWithCurrentItem" Value="False"/> </Style> </DataGridComboBoxColumn.ElementStyle> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=DropDownValues, Mode=OneWay}"/> <Setter Property="IsDropDownOpen" Value="True" /> </Style> </DataGridComboBoxColumn.EditingElementStyle> </DataGridComboBoxColumn> <DataGridTextColumn Header="Column 2" Binding="{Binding Path=Col2, Mode=TwoWay, ValidatesOnDataErrors=True}" Width="*"/> </DataGrid.Columns> </DataGrid> 

I can’t imagine what I am doing wrong. I saw this other link that seems to describe the same problem as mine, but the solution that worked for them does not seem to work for me; I added SelectedValueBinding and SelectedValuePath, but the behavior has not changed.

+4
source share
1 answer

Remove Mode=TwoWay from the bindings.

The problem is caused by an error in the clipboard and automation support. This works by setting a special property in the cell to ClipboardContentBinding , and then reading the value. If this binding is two-way, it terminates, sometimes pushing the old value from the special property back into the view model, and validation errors seem to cause this behavior. DataGridBoundColumns and DataGridComboBoxColumns will supply Binding or SelectedItemBinding if ClipboardContentBinding is null, so you will get this error if you set either of them to TwoWay binding.

If you do not set Mode , it will be Default and will use the default value for the TwoWay property for TextBox.Text and ComboBox.SelectedItem , but OneWay for the special clipboard property.

+4
source

All Articles