-2

I have a problem with checkbox. I'm trying to explain.

My Datagrid can have multiple row, so multiple checkbox. When one or many checkbox is true, so a button IsEnable=true. But if no checkbox is true, so the button is disable.

Maybe an issue with a count of checkbox IsChecked ? Or maybe the click processus is not appropriate?

Here is my code :

private void Chk_UID_IsSelected_Click(object sender, RoutedEventArgs e)
{
    var Chkbox = sender as CheckBox;

    if (Chkbox.IsChecked == true)
    {
        UID_Disconnect.IsEnabled = true;
    }
    else
    {
        UID_Disconnect.IsEnabled = false;
    }
}

But my code check only one checkbox in fact. If i click on 2 checkbox and uncheck one, my button have an incorrect state.

My design :

row with checkbox

Another try with no result:

private void Chk_UID_IsSelected_Click(object sender, RoutedEventArgs e)
{
    foreach (CheckBox c in dgConnected_Users.ItemsSource)
    {
        var Chkbox = sender as CheckBox;
        UID_Disconnect.IsEnabled = Chkbox.IsChecked == true;
    }
}

Here another try with no result:

private void Chk_UID_IsSelected_Click(object sender, RoutedEventArgs e)
{
    int checkedBoxes = dgConnected_Users.Items
        .OfType<CheckBox>().Count(c => (bool)c.IsChecked == true);

    if (checkedBoxes > 0)
    {
        // You shall pass!
        UID_Disconnect.IsEnabled = true;
    }
    else
    {
        // None shall pass
        UID_Disconnect.IsEnabled = false;
    }
}
elena.kim
  • 930
  • 4
  • 12
  • 22
  • Because you are only checking the status of the last checkbox clicked. You will need to loop through all the checkboxes in the datagrid. – Ryan Thomas May 18 '21 at 19:05
  • Every time a `CheckBox` is clicked, you are setting the `IsEnabled` property of your button based on the value of only the current `CheckBox`. You need to check whether any other checkboxes are also checked before you set the `IsEnabled` property to `false`. – D M May 18 '21 at 19:06
  • It's here my problem, i don't find a way to do a loop through all the checkboxes. – Fabrice Bertrand May 18 '21 at 19:08
  • You should [iterate the data source](https://stackoverflow.com/questions/1295023/wpf-datagrid-how-do-you-iterate-in-a-datagrid-to-get-rows-and-columns) to check if other `CheckBox`es are checked. If the `CheckBox`es are not bound to your data source, you can [iterate the `DataGrid`'s `ItemCollection` directly](https://stackoverflow.com/questions/39443793/iterating-through-rows-of-a-data-grid-wpf). – D M May 18 '21 at 19:12
  • Edit your existing question and paste your updated code THERE and save. Dont post code in a comment to your existing questions. – DRapp May 18 '21 at 20:23

1 Answers1

0

OK I have progress in my code, here the new one :

        private void Chk_UID_IsSelected_Click(object sender, RoutedEventArgs e)
    {

        foreach (DataRowView dr in dgConnected_Users.ItemsSource)
        {
            var Chkbox = sender as CheckBox;

            if (Chkbox.IsChecked == true)
            {
                isAnyChecked++;
            }
            else
            {
                isAnyChecked--;
            }
        }

        if (isAnyChecked > 0)
        {
            UID_Disconnect.IsEnabled = true;
        }
        else
        {
            UID_Disconnect.IsEnabled = false;
        }
    }

That do the staff, but i know that if I check just one checkbox, 'isAnyChecked' = 2, or it must be equal 1.