4

I have a DataGrid with two columns:

  1. DataGridComboBoxColumn
  2. DataGridTextColumn.

I have set up data validation so that if one has a value, the other will be in error until it also has a value. The validation is silly, but it provides some simple criteria with which to do validation so I can illustrate this issue.

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

Can anyone help please? 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'm doing wrong. I saw this other link that seems to describe the same problem I am having, but the solution that worked for them doesn't seem to work for me; I added the SelectedValueBinding and SelectedValuePath, but the behavior did not change.

Community
  • 1
  • 1
Matt Zappitello
  • 785
  • 2
  • 11
  • 30

1 Answers1

6

Remove Mode=TwoWay from the bindings.

The problem is caused by a bug in the clipboard and automation support. That works by setting a special property on the cell to ClipboardContentBinding and then reading the value. If that binding is two-way, it winds up sometimes pushing an old value from the special property back to the view model, and validation errors seem to trigger this behavior. DataGridBoundColumns and DataGridComboBoxColumns will supply Binding or SelectedItemBinding if ClipboardContentBinding is null, so you’ll get this bug if you set either of those to a TwoWay binding.

If you don’t set Mode, it will be Default and use the default from the property, which is TwoWay for TextBox.Text and ComboBox.SelectedItem but OneWay for the special clipboard property.

Quartermeister
  • 57,579
  • 7
  • 124
  • 111
  • I don't understand what is going on with this `ClipboardContentBinding`and what property is affected by it but removing `Mode=TwoWay` fixed the problem for me. Sooo...thank's I guess. – Steffen Winkler Jul 28 '16 at 08:59
  • I'd love more info on this bug, and whether ClipboardContentBinding can be manipulated itself to cause or remove it. – Ryan Leach Oct 23 '18 at 11:58