tags:

views:

123

answers:

3

I use checkbox in WPF window. I use some logic in unchecked event of checkbox. And I want to cancel uncheck if need it in the body of this event. How can I do this?

    private void chApprove_Checked(object sender, RoutedEventArgs e)
    {
        btnAddDepartment.IsEnabled = true;
        brnRemoveDepartment.IsEnabled = true;
    }

    private void chApprove_Unchecked(object sender, RoutedEventArgs e)
    {
        if (lbSource.Count == 0)
        {
            btnAddDepartment.IsEnabled = false;
            brnRemoveDepartment.IsEnabled = false;
        }
        else
        {
            MessageBox.Show("Staff already in use! Release it first from dependecies!");
            CheckBox myCheckBox = e.Source as CheckBox;
            myCheckBox.IsChecked = true;


        }
    }

Impossible to cancel uncheck !!!

+1  A: 
void CheckBox1_Unchecked(object sender, RoutedEventArgs e)
{
    if(ResultOfSomeLogic)
    {    
        CheckBox myCheckBox = e.Source as CheckBox;
        myCheckBox.IsChecked = True; // Check it again
    }
    else
    {
    }
}

Also take a look at EventToCommand Binding Behaviour in MVVM Light to take advantage of CanExecute method.

Veer
It not work as expected. Some strange behavior: uncheked occured and impossible to check it again in UI when I use this code.
Polaris
You aren't doing anything in your Checked event right?
Veer
I edited my question.
Polaris
Your answer help me. It works for context Menu
Polaris
A: 

You could do this easily with an attached behavior (rather than using code behind), you can take a look at this answer if you need a sample of how to structure one (it's only a few lines of code).

My spider-sense is telling me this isn't a very good idea though - I can't imagine a way to "justify" rechecking a checkbox that a user has clicked, it just strikes me as very jarring. Can you not either bind the enabled state of the checkbox to a property on your ViewModel or, if you have an ICommand bound to it, use the CanExecute delegate to enable/disable it based on the same logic?

Steven Robbins
My checkbox bound to class property with two way databinding (INotifierPropertyChanged also implemented on a class). But manipulating with value of property doesn't help me
Polaris
Doesn't really answer my question though - why can't you just enable/disable the checkbox with an additional property on the VM? Then if you are in a state where you aren't "allowed" to uncheck (through whatever logic) the checkbox will be disabled. Seems much cleaner than reverting user input!
Steven Robbins
A: 

Bind the IsChecked property of check box. Like

IsChecked="{Binding IsChecked, Mode = TwoWay}"

and in your class define some thing like dis;

private bool isChecked;

public bool IsChecked
{
  get
   {
     return this.isChecked;
   }

  set
  {
    this.isChecked = value;
    OnPropertyChanged["IsChecked"];
  }

}

and in your event

void CheckBox1_Unchecked(object sender, RoutedEventArgs e)
{
    if(ResultOfSomeLogic)
    {    
        this.IsChecked = true;
    }
    else
    {
    }
}

hope this will work for u..

Good Luck..

Johnny