mardi 8 septembre 2020

How to add Checked and Unchecked in the DataGridCheckBoxColumn ( C# WPF )?

I'm working on a C# WPF project and I'm searching for a trick to add Uncheck and Check events to the datagridCheckboxColumn , I have already found many solutions but I'm haven't got what I need exactly.

My object is to color every row checked with Gray and to color every row unchecked by user to white ( the original color of the datagridView).

The event of the checked row is worked perfectly and the row get colored when I check the datagridCheckboxColumn.

I'm searching for a solution for the unchecked event.

XAML Code

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

C# Code


 private void OnChecked(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < datagridView.Items.Count; i++)
            {
                DataGridRow row = (DataGridRow)datagridView.ItemContainerGenerator.ContainerFromIndex(i);
                CheckBox ch = (CheckBox)datagridView.Columns[0].GetCellContent(row);
                bool ischecked = (bool)ch.IsChecked;

                if (ischecked)
                {
                    row.Background = Brushes.Gray;

                }
                
            }
        }




Aucun commentaire:

Enregistrer un commentaire