poco
poco

Reputation: 3005

InvalidOperationException - A TwoWay or OneWayToSource binding cannot work on the read-only property

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?

Upvotes: 16

Views: 16599

Answers (4)

marsh-wiggle
marsh-wiggle

Reputation: 2813

For those who find this without using PropertyChanged

Regardless of whether PropertyChanged is used, this exception is also thrown when you have a calculated property (without setter) and the user tries to edit the column. Setting the whole DataGrid to IsReadOnly="True" or just the column to ReadOnly is enough then.

Upvotes: 0

Flot2011
Flot2011

Reputation: 4671

In my absolutely stupid case, I have forgotten to define a setter for a property, making it, well, read-only. Just my 2 cents for those who work too late.

Upvotes: 0

insipid
insipid

Reputation: 3308

Either make your setter public or explicitly set the Binding.Mode to OneWay.

Upvotes: 23

Dominik
Dominik

Reputation: 3372

Your setter is private, either specify the binding to be mode OneWay or remove the private from the setter

Upvotes: 3

Related Questions