Reputation: 139
I want a select all CheckBox
which when Checked
, checks all other CheckBoxes
and when Unchecked
, unchecks the others. I have written this code:
private void all_CheckedChanged(object sender, RoutedEventArgs e)
{
if (all.IsChecked == true)//this is working
{
issue.IsChecked = true;
summary.IsChecked = true;
type.IsChecked = true;
status.IsChecked = true;
label.IsChecked = true;
components.IsChecked = true;
empty.IsChecked = true;
reporter.IsChecked = true;
requester.IsChecked = true;
team.IsChecked = true;
assignee.IsChecked = true;
}
else if (all.IsChecked == false)//this is not doing anything
{
issue.IsChecked = false;
summary.IsChecked = false;
type.IsChecked = false;
//same for other checkboxes
}
}
when I debug it I see that the event takes places when I check the CheckBox
but nothing happens when I uncheck it. Someone, please suggest me how to do this in case I am doing something wrong.
Upvotes: 2
Views: 1849
Reputation: 39946
In WPF's CheckBox
you have not the Changed
event. You have used the Checked
event so you have to specify the Unchecked
also and attach the same handler to both events. So your XAML code should be like this:
<CheckBox Name="all" Checked="All_OnChecked" Unchecked="All_OnChecked"></CheckBox>
However I strongly recommend that you use MVVM pattern for this purposes.
Upvotes: 1
Reputation: 607
This is assuming you already have the checkboxes in the XAML file.
Be sure to add the Checked
and Unchecked
events to the SelectAll
checkbox in the XAML file.
Then, populate what you want each event to do. In your case, you want the Checked
event to check all the other checkboxes and the Unchecked
event to do the opposite.
Try this...
private void SelectAll_Checked(object sender, RoutedEventArgs e)
{
issue.IsChecked = true;
// Do the rest for all other checkboxes you want to check
}
private void SelectAll_Unchecked(object sender, RoutedEventArgs e)
{
issue.IsChecked = false;
// Do the rest for all other checkboxes you want to uncheck
}
<CheckBox x:Name="SelectAll" Content="Select All" HorizontalAlignment="Left" Margin="107,125,0,0" VerticalAlignment="Top"
Checked="SelectAll_Checked"
Unchecked="SelectAll_Unchecked" />
<!-- Other checkboxes ... -->
Check out my Gist link for this example.
Upvotes: 3