Page 1 of 1

Two way binding not working on read-only properties?

Posted: 10 May 2023, 04:37
by yuluckyvr
Hello guys



Like to get an understanding of why this happens here:
Im working in C# in Unity, I have this control in XAML file

<CheckBox Content="BooleanBinding Test" x:Name="TestCheckbox" IsChecked="{Binding AutoBoolValue, Mode=TwoWay}"/>

The data context of TestCheckbox is set to this class BindingContainer_Bool
Where the IsChecked property is binding to the class's AutoBoolValue property
Where the getter and setter are point to a bool member variable inherited from parent class
Code of this class can be seen below
 public class BindingContainer_Bool: DataContainer_Bool,INotifyPropertyChanged
  {

      public bool AutoBoolValue => Value;
    

        protected override void OnValueChanged(bool newValue)
        {
            base.OnValueChanged(newValue);
            OnPropertyChanged(nameof(AutoBoolValue));
        }


        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
 }

It is throwing an error in Unity
"[NOESIS/E] A TwoWay or OneWayToSource Binding cannot work on a read-only property"

I checked the NoesisGUI documentation, the CheckBox's IsChecked property is not read only

Anyone has idea on this?

Re: Two way binding not working on read-only properties?

Posted: 10 May 2023, 10:47
by maherne
The error is referring to the bound DataContext property AutoBoolValue, which is defined as a read-only expression.

For writable properties you need an expression for get and set.
   public bool AutoBoolValue
   {
      get => Value;
      set => Value = value;
   }

Re: Two way binding not working on read-only properties?

Posted: 10 May 2023, 11:50
by sfernandez
As Mark indicated the error message is referring to the view model property AutoBoolValue, that is read-only. We need to improve the message to make this more clear, something like:
A TwoWay or OneWayToSource binding cannot work on the read-only property 'AutoBootValue' of type 'NS.BindingContainer_Bool'
Could you please report this in our bugtracker?

Re: Two way binding not working on read-only properties?

Posted: 17 May 2023, 03:42
by yuluckyvr
The above suggestion works.
And yes the error message is quite misleading

Re: Two way binding not working on read-only properties?

Posted: 17 May 2023, 15:04
by jsantos
Thanks! Ticket #2602 created to improve the error message.