开发者

Cancel uncheck in WPF

开发者 https://www.devze.com 2023-01-01 10:05 出处:网络
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?

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;
        brnRemove开发者_开发百科Department.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 !!!


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.


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?


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..


In my case, I could not use a solution that allowed unchecking in the first place. If the checked state initiates a critical asynchronous operation, it is not always ideal to uncheck just to check it again: Why allow cancelling this operation if it shouldn't have been allowed to cancel in the first place?

For MenuItems, you can subscribe to the PreviewMouseDown event and set IsCheckable to false; then subscribe to the Click event and set IsCheckable back to true. The reason this works is because IsCheckable just determines whether or not to initiate the state change, unlike IsHitTestEnabled="false" and IsEnabled="False", which will stop all events from firing.

If you try to disable it, no subsequent events will fire making it impossible to restore checkability; by making it uncheckable beforehand, we avoid this mistake. Click also happens to occur after the state would've been changed so it works out quite nicely.

Unfortunately, CheckBox does not have an equivalent IsCheckable property; however, the same concepts described above (i.e., PreviewMouseDown, Click pattern) can produce a similar, if not identical, result.


Well assuming a check box is intended to interact with users instead of programmatic ways, there's a simple way to cancel Unchecked events based on some logic when user hits left mouse button or space bar:

private void CheckBox_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // If it's already checked so next click would uncheck it hence trigger the Unchecked event.
    if ((sender as System.Windows.Controls.CheckBox).IsChecked == true)
    {
        var isConfirmed = false; // Use your confirmation logic here instead.

        // If e.Handled is set to false that will cancel further events such as the Unchecked event.
        e.Handled = isConfirmed;
    }
}

private void CheckBox_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    // If it's already checked so when user presses space bar it would uncheck it hence trigger the Unchecked event.
    if ((sender as System.Windows.Controls.CheckBox).IsChecked == true)
    {
        if (e.Key == System.Windows.Input.Key.Space)
        {
            var isConfirmed = false; // Use your confirmation logic here instead.

            // If e.Handled is set to false that will cancel further events such as the Unchecked event.
            e.Handled = isConfirmed;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消