views:

352

answers:

1

Hello!

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.

+1  A: 

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.

levanovd
Hello !I thought OnPropertyChanged () would trigger the getter and not the setter again. But thank you your answer it lead me to a solution.Used Validation as suggested. When setter was not changing underlying value i created an errormessage. When returning Validation errormessage, deleted errormessage and called OnPropertyChanged(). --> getter is called with the correct value in it --> CheckBox stays unchecked.