Question

i have a simple checkbox

<CheckBox IsChecked="{Binding ForceInheritance}"/>

In Code i have a class with "INotifyPropertyChanged" and the Property

public bool ForceInheritance
{
  get { return forceInheritance; }

  set
  {
    if (forceInheritance != value)
    {
      value = SomeTest();

      if (forceInheritance != value)
      {
        //something is done here.
      }

      OnPropertyChanged("ForceInheritance");
    }
  }
}

If SomeTest() returns !value so the underlying data does not have to change the CheckBox still changes its IsChecked state.

For example:
ForceInheritance is false.
CheckBox is clicked.
SomeTest() returns false.
--> The underlying data is not changed
--> I would expect the CheckBox to stay unchecked
The CheckBox gets checked.

What can i do to not change the IsChecked state of the CheckBox when the setter does not actually change the value?

I thought the "OnPropertyChanged("ForceInheritance");" would do the trick but it didn't.

Thank you for your time.

Was it helpful?

Solution

This problems occurs because when you click checkbox it s value is changed. Then binding causes property set. There you manipulate with its value and hope that calling OnPropertyChanged will cause back binding action updating checkbox value. But doesn't work because updating control after updating property may produce infinite updating loop. To prevent this PropertyChanged is ignored.

What can you do is to move some logic of your property setter to binding validation rule. You can read here about validation rules or leave a comment if you need more information or examples.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top