Yituo
Yituo

Reputation: 1576

How to block a checkbox to be checked in on a Checked event when some condition is not met?

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

Answers (2)

Tejus
Tejus

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

sujith karivelil
sujith karivelil

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

Related Questions