A TwoWay or OneWayToSource binding cannot work on the read-only property
Asked Answered
T

1

124

I've a read only property I need to display in a textbox, and getting this error at runtime. I've set IsEnabled="False", IsReadOnly="True" - no luck. Other searches say the readonly should fix it, but not for me. I've got an ugly workaround by adding a dummy setter...

Tomboy answered 26/2, 2009 at 12:6 Comment(3)
If you do want two way binding, the property setter must be public. See #8773650Girdle
And from the department of the blindingly obvious, the setter must also exist; i.e. a property with just a get will exhibit the same issue.Clarissaclarisse
@ColonelPanic "I've a read only property I need to display in a textbox" tells me that the asker isn't trying to do a two-way binding. I think he hadn't specified the binding mode and so it defaulted to TwoWay.Longboat
I
209

It's hard to guess without code, but you should be able to set the BindingMode to OneWay.

<TextBox Text="{Binding Path=MyProperty, Mode=OneWay}" />

or from code:

Binding binding = new Binding();
binding.Mode = BindingMode.OneWay;
Iodism answered 26/2, 2009 at 12:14 Comment(4)
Yep, "Mode=OneWay" == Read Only; "Mode=OneWayToSource" == Write OnlyJobholder
Please note that in .NET 4.0 there's a "bug" that OneWayToSource also does a get: #14968167Krohn
Also, for TwoWay Binding on Settings see https://mcmap.net/q/182211/-bind-to-a-value-defined-in-the-settingsPublicspirited
This together with #871393 solved my problem. Thanks! One thing remains unclear: Why does my app run fine within Visual Studio despite this issue?Longboat

© 2022 - 2024 — McMap. All rights reserved.