Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a WPF check box control toggle event?

Considering WPF controls, how do I know if a check box's value has changed (toggled)?

I know there are the common Checked, Unchecked, Clicked events, but how about an event for when the value changes, regardless of how it was changed?

I looked through the events and I didn't find anything, but maybe I'm missing the obvious (as it has happened many times in the past).

like image 833
IneedHelp Avatar asked Dec 06 '22 12:12

IneedHelp


2 Answers

You can just bind IsChecked Dependency Property to a boolean. On that binded property setter you can manipulate what you want (independently if it's setting it to true or false). That works just as expected.

On your view:

  <Grid>
    <CheckBox ... IsChecked="{Binding ShowPending}"/>
  </Grid>

On your DataContext ViewModel or CodeBehind.

  private bool showPending = false;

  public bool ShowPending
  {
      get { return this.showPending; }
      set 
      { 
         //Here you mimic your Toggled event calling what you want!
         this.showPending = value; 
      }
  }
like image 196
Erre Efe Avatar answered Dec 10 '22 03:12

Erre Efe


I know this already has an accepted answer, but binding is a bit overkill on this.

Just write one event handler and wire it to both the Checked and Unchecked events then check the IsChecked property inside your event handler.

like image 40
Malchia7 Avatar answered Dec 10 '22 01:12

Malchia7