yuluckyvr
Topic Author
Posts: 3
Joined: 10 May 2023, 03:14

Two way binding not working on read-only properties?

10 May 2023, 04:37

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?
 
User avatar
maherne
Site Admin
Posts: 41
Joined: 01 Jul 2022, 10:10

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

10 May 2023, 10:47

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;
   }
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

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

10 May 2023, 11:50

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?
 
yuluckyvr
Topic Author
Posts: 3
Joined: 10 May 2023, 03:14

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

17 May 2023, 03:42

The above suggestion works.
And yes the error message is quite misleading
 
User avatar
jsantos
Site Admin
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

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

17 May 2023, 15:04

Thanks! Ticket #2602 created to improve the error message.

Who is online

Users browsing this forum: Google [Bot], vinick and 56 guests