IneedHelp
IneedHelp

Reputation: 1758

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

Upvotes: 4

Views: 14091

Answers (4)

Malchia7
Malchia7

Reputation: 61

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.

Upvotes: 6

sellmeadog
sellmeadog

Reputation: 7517

Your best option is probably the IsChecked property. But if you require an event, you can look at creating a DependencyPropertyDescriptor and registering a handler with the AddValueChanged method.

I think this is about as close as you'll get to an evented notification that the check box's value has changed. Creating the descriptor and adding the handler looks something like this:

var dpd = DependencyPropertyDescriptor.FromProperty(CheckBox.IsChecked, typeof(CheckBox));
dpd.AddValueChanged(...);

Upvotes: 1

tylerjgarland
tylerjgarland

Reputation: 643

Going off Randolf's answer, just create a class representing your window. In the new class, create a property called BlahIsChecked. Implement the INotifyPropertChangedEvent in the class and in the setter of the the new property, fire the event with the property name.

class Blah : INotifyPropertyChanged
{
    // Used for triggering the event
    public event PropertyChangedEventHandler PropertyChanged;

    // Called when the property changes
    protected void OnPropertyChanged(String propertyName)
    {
        // Retrieve handler
        PropertyChangedEventHandler handler = this.PropertyChanged;
        // Check to make sure handler is not null
        if(handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private bool _blahIsChecked;
    public bool BlahIsChecked
    {
        get {
            return _blahIsChecked;
        }
        set {
            _blahIsChecked = value;
            OnPropertyChanged("BlahIsChecked);
        }
    }
}

Now, go to your wpf class and say this.DataContext = new MainModel(); You can do this in WPF or c#.

Now in your checkbox xaml do the following

<checkbox Checked="{Binding BlahIsChecked, Mode=TwoWay}"/>

I did this from memory but should get you started. Good luck.

Upvotes: 1

Erre Efe
Erre Efe

Reputation: 15557

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; 
      }
  }

Upvotes: 7

Related Questions