0

I face a strange issue with an OnClick event in DataGridCheckBoxColumn which is not firing on the very first check/unchecked click. Here are some code samples from the xaml, code-behind and view model:

XAML

<DataGrid.Columns>
    <DataGridCheckBoxColumn Width="1*"
                            Binding="{Binding IsAssigned, UpdateSourceTrigger=PropertyChanged}"
                            IsReadOnly="False">
        <DataGridCheckBoxColumn.CellStyle>
            <Style>
                <EventSetter Event="CheckBox.Click"
                             Handler="OnClick" />
            </Style>
        </DataGridCheckBoxColumn.CellStyle>
    </DataGridCheckBoxColumn>
</DataGrid.Columns>

The "IsAssigned" is a bool property that is available in the binded Object. That works absolutly fine.

Code behind

public void OnClick(object sender, RoutedEventArgs e)
{
    // Retrieving the parameter object from sender
    // and doing some other stuff here...
    // ...
    viewModel.ObjectsToChange(parameterObjectFromDataGridCell);
}

View model

internal void ObjectsToChange(object parameterObjectFromDataGridCell)
{
    // do some stuff
}

Why not using OnChecked/OnUnchecked events?

I do not use the OnChecked or OnUnchecked events because they get triggered during load of the DataGrid. As I did not find a solution to differentiate between the initial load and a real Checked/Unchecked event triggered by the user, I decided to try to OnClick event.

Question:

Why does it ignore the very first check/uncheck click? Any further clicks are recognized.

rekcul
  • 319
  • 1
  • 7
  • 18
  • https://stackoverflow.com/questions/3833536/how-to-perform-single-click-checkbox-selection-in-wpf-datagrid – ASh Dec 07 '22 at 08:27
  • The proposed question is quite similar but not equal to my issue. In my case: yes the first click is selecting the datagrid row but I dont care about that click. The very first check/uncheck click is also ignored and no event is triggered. – rekcul Dec 07 '22 at 08:49

1 Answers1

0

Replace the DataGridCheckBoxColumn with a DataGridTemplateColumn

<DataGridTemplateColumn Width="1*" IsReadOnly="False">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsAssigned, UpdateSourceTrigger=PropertyChanged}"
                      Click="OnClick"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

It's the former that causes the behaviour you are experiencing.

mm8
  • 163,881
  • 10
  • 57
  • 88