InvalidOperationException - A TwoWay or OneWayToSource binding cannot work on the read-only property
Solution 1
Either make your setter public or explicitly set the Binding.Mode
to OneWay
.
Solution 2
Your setter is private, either specify the binding to be mode OneWay or remove the private from the setter
poco
Updated on June 11, 2022Comments
-
poco almost 2 years
I'm using the MVVM pattern and am receiving the following when i run my app
InvalidOperationException A TwoWay or OneWayToSource binding cannot work on the read-only property 'Options' of type 'ViewModel.SynergyViewModel'.
I have commented all my source out in my view model and have traced this back to a check box. If i comment out the the checkbox or the properity in my view model the app runs, minus the functionality. Below i have listed the code for my checkbox and the property within the viewmodel.
<CheckBox Grid.Column="4" HorizontalAlignment="Right" Margin="5,0,5,5" IsChecked="{Binding Options}" Content="Options"/>
private bool _Options; public bool Options { get { return _Options; } private set { if (_Options == value) return; _Options = value; OnPropertyChanged("Options"); } }
System.InvalidOperationException occurred Message=A TwoWay or OneWayToSource binding cannot work on the read-only property 'Options' of type 'ViewModel.MyViewModel'. Source=PresentationFramework StackTrace: at MS.Internal.Data.PropertyPathWorker.CheckReadOnly(Object item, Object info) InnerException:
Any ideas on what i'm what i'm missing here?
-
Jordan almost 11 yearsIf you're developing a .net 4.0 WPF project on a machine with .net 4.5 installed, you will not run into this error during development! Microsoft needs to fix this.
-
Omaer over 7 yearsIt's so strange that I'm getting the same error with an internal setter... I would have assumed an internal setter would be allowed, but strangely not.
-
user0474975 almost 7 yearsI wish this answer specified HOW to set the Binding.Mode to OneWay - where do I do this? In the set?
-
dibs487 almost 7 yearsIsChecked="{Binding Options, Mode=OneWay}"