States
States

Reputation: 163

Uncheck not working on checkbox

I have a checkbox that should act when it is checked and when it is clicked to uncheck. When it is checked, it should enable/make sure that other checkboxes are checked, and when it is unchecked, the other checkboxes should uncheck. It works fine when it is checked. But when it is unchecked, nothing happens. So I am missing something like an "onChange" property.

What could be the problem in my code

<CheckBox Content="Check all" Grid.Column="1" Grid.Row="1" Margin="200 7 0 0" Name="IsCheckedCheckAll" Checked="IsCheckedCheckAll_Checked"/>

if (IsCheckedCheckAll.IsChecked == true)
        {
            if (IsCheckedCheckAll.Content.Equals("Check all"))
            {
                IsCheckedCheckAll.Content = "Uncheck all";

                Exc1.IsChecked = true;
                Exc2.IsChecked = true;
                Exc3.IsChecked = true;
                Exc4.IsChecked = true;
                Exc5.IsChecked = true;
            }
        }
        else
        {
            if (IsCheckedCheckAll.IsChecked == false)
            {
                IsCheckedCheckAll.Content = "Check all";

                Exc1.IsChecked = false;
                Exc2.IsChecked = false;
                Exc3.IsChecked = false;
                Exc4.IsChecked = false;
                Exc5.IsChecked = false;
            }
        }

Upvotes: 0

Views: 1758

Answers (1)

dharshana jagoda
dharshana jagoda

Reputation: 430

You are missing the Unchecked event handler.

so change the XAML to

  <CheckBox Content="Check all" Grid.Column="1" Grid.Row="1" Margin="200 7 0 0" Name="IsCheckedCheckAll" Checked="IsCheckedCheckAll_Checked" Unchecked="IsCheckedCheckAll_Checked"/>

Its better to use bindings to do this instead.

1.Remove the event handlers (Checked, Unchecked) from the IsCheckedCheckAll checbox

2.The other Checkboxes can bind to the IsChecked property of the IsCheckedCheckAll by using the binding expression IsChecked="{Binding IsChecked, ElementName=IsCheckedCheckAll, Mode=OneWay}"

 <CheckBox Content="Check all" Grid.Column="1" Grid.Row="1" Margin="200 7 0 0" Name="IsCheckedCheckAll" />

   <CheckBox Content="Exc1" Name="Exc1" IsChecked="{Binding IsChecked, ElementName=IsCheckedCheckAll, Mode=OneWay}" />

One way where you can change the contents to Checked All, Unchecked all through bindings is to map the boolean IsChecked property to some string content. You can do this by writing a Value Converter like the one below.

    namespace WpfApplication1
    {
        public class CheckedStatusToTextConverters : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                bool checkedStatus = (bool)value;
                return checkedStatus ? "Uncheck All" : "Check All";

            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotSupportedException();
            }
        }
    }

Then in XAML you can use the Value Converter like this

<Window.Resources>
    <mycode:CheckedStatusToTextConverters x:Key="MyConverter" />
</Window.Resources>

<Grid>
    <CheckBox Content="{Binding IsChecked, Converter={StaticResource MyConverter}, RelativeSource={RelativeSource Self}}"/>

Upvotes: 3

Related Questions