Search code examples
c#wpfdatagridcomboboxcolumn

WPF : How to add Checked and Unchecked events in the DataGridCheckBoxColumn?


WPF : How to add Checked and Unchecked events in the DataGridCheckBoxColumn?

<DataGridCheckBoxColumn Header="Choose" x:Name="choose">
    <DataGridCheckBoxColumn.CellStyle>
        <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
            <EventSetter Event="CheckBox.Checked" Handler="OnChecked"/>
            <EventSetter Event="CheckBox.Unchecked" Handler="OnChecked"/
        </Style>
     </DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>

enter image description here


Solution

  • Your XAML works fine the problem lies withing you getting the Check Box. You should be able to access the element from the Check Box you triggered with the Unchecked or Checked Event.

    Example:

    var ch = sender as Checkbox;
    var row = data_kala.ItemContainerGenerator.ContainerFromItem(ch) as DataGridRow;
    bool ischecked = ch.IsChecked;
    
    if (ischecked) {
        row.BackGround = Brushes.Gray;
    }
    else {
        row.BackGround = Brushes.White;
    }