Reputation: 1576
I'm using System.Windows.Controls.CheckBox
. I am implement the OnChecked
event handler, I want the checkbox to be unchecked if some condition is not met after the handler runs, how should I implement the handler?
xaml code:
<CheckBox Checked="OnChecked" >Checkbox text</CheckBox>
C# code:
private void OnChecked(object sender, RoutedEventArgs e)
{
// Block checkbox being checked if some condition not met
}
Upvotes: 0
Views: 238
Reputation: 724
In the OnChecked event handler, you can add an if block to check if your conditions have met. If the condition is not satisfied, you can clear the IsChecked property.
private void OnChecked(object sender, RoutedEventArgs e)
{
if (true) // your condition
{
((CheckBox) sender).IsChecked = false;
}
}
Upvotes: 0
Reputation: 29036
You can try something like this:
private void OnChecked(object sender, RoutedEventArgs e)
{
if(*your condition*)
{
(sender as System.Windows.Controls.CheckBox).IsChecked = false;
}
}
Upvotes: 1