Mike-Kilo
Mike-Kilo

Reputation: 1312

How to fire an event everytime CheckBox.IsChecked is assigned a value?

I am facing the following problem: for a checkbox in WPF I need to know, when an attempt was made to change the state of the checkbox, i.e.:

This construction is to be used in a piece of software that controls an embedded device. It is not possible to request the state of this device, it is only possible to tell to the device to enter certain state. If the device is in certain state, telling it again to enter this state makes no harm. Hence, my request.

Up to now, I've come up with the following solution: write a new class which derives from CheckBox, but redefine IsChecked property in the following way:

public class ExtendedCheckBox : CheckBox
{
    public class IsCheckedAssignedEventArgs : RoutedEventArgs
    {
        public bool? prevState { get; set; }
        public bool? newState { get; set; }
        public IsCheckedAssignedEventArgs(bool? prevState, bool? newState)
        {
            this.prevState = prevState;
            this.newState = newState;
        }
    }

    public event EventHandler IsCheckedAssigned = delegate { };

    public new bool? IsChecked
    {
        get
        {
            return base.IsChecked;
        }

        set
        {
            this.IsCheckedAssigned(this, new IsCheckedAssignedEventArgs(this.IsChecked, value));
            base.IsChecked = value;
        }
    }
}

It works well only if the IsChecked property is assigned programmatically. The event is not fired, when the CheckBox is clicked.

The first question is - is this the correct approach for my problem? If yes, how to make it work for all cases I need? If no, what is a better approach?

Thanks in advance for any ideas.

Upvotes: 1

Views: 1714

Answers (4)

Nitin Purohit
Nitin Purohit

Reputation: 18580

IsChecked property on CheckBox is a DependancyProperty. Just creating a new wrapper over it is not going to update it via UI as WPF engine will call the GetValue() and SetValue() to get and set its value not your property wrappers.

If you want to just get to know if you IsChecked is changed then you can override the propertymeta data for IsCheckedProperty by adding below code in your class.

    static ExtendedCheckBox ()
    {
        IsCheckedProperty.OverrideMetadata(typeof(ExtendedCheckBox ), new FrameworkPropertyMetadata(null , new CoerceValueCallback(IsCheckedCoerced)));
    }

    private static object IsCheckedCoerced(DependencyObject obj, object value)
    {
        var myCheckBox = obj as ExtendedCheckBox ;

        if (myCheckBox != null)
        {
            myCheckBox.IsCheckedAssigned(myCheckBox, new IsCheckedAssignedEventArgs(myCheckBox.IsChecked, (bool)value));
        }
        return value;
    }

Upvotes: 2

Rocky
Rocky

Reputation: 4524

I am using checkbox something like below in my project

<CheckBox Name="chkExist" Grid.Column="1" Grid.Row="0" Content="Existing settings" VerticalAlignment="Center" IsChecked="{Binding IsExistingSetting, Mode=TwoWay}"></CheckBox>

bool isExistingSetting;

public bool IsExistingSetting
{
  get { return isExistingSetting; }
  set
  {
    if (value != isExistingSetting)
    {
     //calling one method as per my business. Method(value);
      isExistingSetting= value;
      OnPropertyChanged("IsChecked");
     }
    }
 }
 private void Method(bool isValue)
 {
  if(isValue)
  {
    // do something...
  }
  else
  {
   // do something...
  }
}

Upvotes: 0

mohammad jannesary
mohammad jannesary

Reputation: 1821

Override OnClick ; OnChecked and OnUnchecked events in your class and raise your new event;

Upvotes: 0

Nickolai Nielsen
Nickolai Nielsen

Reputation: 942

if you databind to the property IsChecked it will set the value on your bind target every time it is changed

Upvotes: 1

Related Questions