I have a group of WPF CheckBoxes that have a Checked event handler that handles some logic when the user clicks on one of them. However, I also have a "Clear All" button that, when clicked, will clear all the checkboxes. If the user clicks on the "Clear All" button, I do NOT want the checkboxes to fire their Checked event. Is there a way of doing this?
You may use the 'Click' event instead of 'Checked' or 'Unchecked' events.
'Click' will ensure that the state change will come from the user input (via mouse button or space bar for instance).
And operations like cb.IsChecked = value
wont fire the 'Click' event.
Remove the event handler at the beginning of the in the Clear All button's event handler and then re-add the event handler at the end of the Clear All button's event handler.
Here's a dirty sample:
XAML
<Window x:Class="UncheckedTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<CheckBox Height="16" Margin="22,30,136,0" Name="checkBox1" VerticalAlignment="Top"
Unchecked="checkBox1_Unchecked">CheckBox 1</CheckBox>
<CheckBox Height="16" Margin="22,76,136,0" Name="checkBox2" VerticalAlignment="Top"
Unchecked="checkBox2_Unchecked">CheckBox 2</CheckBox>
<CheckBox Margin="22,0,136,121" Name="checkBox3" Height="16" VerticalAlignment="Bottom"
Unchecked="checkBox3_Unchecked">CheckBox 3</CheckBox>
<Button HorizontalAlignment="Right" Margin="0,118,37,121" Name="button1" Width="87"
Click="button1_Click">Uncheck All</Button>
<TextBox Height="74" Margin="22,0,20,13" Name="textBox1" VerticalAlignment="Bottom"
TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" />
</Grid>
</Window>
Code Behind
using System;
using System.Windows;
namespace UncheckedTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private int i = 1;
public Window1()
{
InitializeComponent();
}
void checkBox3_Unchecked(object sender, RoutedEventArgs e)
{
textBox1.Text = i++.ToString() + ". Checkbox 3 Unchecked." + Environment.NewLine + textBox1.Text;
}
void checkBox2_Unchecked(object sender, RoutedEventArgs e)
{
textBox1.Text = i++.ToString() + ". Checkbox 2 Unchecked." + Environment.NewLine + textBox1.Text;
}
void checkBox1_Unchecked(object sender, RoutedEventArgs e)
{
textBox1.Text = i++.ToString() + ". Checkbox 1 Unchecked." + Environment.NewLine + textBox1.Text;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
checkBox1.Unchecked -= checkBox1_Unchecked;
checkBox2.Unchecked -= checkBox2_Unchecked;
checkBox3.Unchecked -= checkBox3_Unchecked;
checkBox1.IsChecked = false;
checkBox2.IsChecked = false;
checkBox3.IsChecked = false;
checkBox1.Unchecked += checkBox1_Unchecked;
checkBox2.Unchecked += checkBox2_Unchecked;
checkBox3.Unchecked += checkBox3_Unchecked;
}
}
}
It would be ideal to disable the event but I don't know how that would be done.
Though it would be just as easy to add a boolean to keep track if whether or not the "Clear All" button was pressed. Then only do the Unchecked code if it wasn't unchecked through the button.
private bool clearAllClicked = false;
private void button_Click(object sender, RoutedEventArgs e)
{
clearAllClicked = true;
checkbox.IsChecked = false;
clearAllClicked = false;
}
private void checkbox_Unchecked(object sender, RoutedEventArgs e)
{
if (!clearAllClicked)
{
//do stuff
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With