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...
-
6If you do want two way binding, the property setter must be public. See stackoverflow.com/questions/8773150/…– Colonel PanicNov 19, 2012 at 15:04
-
1And 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.– noonandOct 22, 2015 at 8:52
-
@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.– StewartDec 22, 2021 at 13:44
Add a comment
|
1 Answer
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;
-
32Yep, "Mode=OneWay" == Read Only; "Mode=OneWayToSource" == Write Only Feb 26, 2009 at 14:40
-
1Please note that in .NET 4.0 there's a "bug" that OneWayToSource also does a get: stackoverflow.com/questions/14967667/…– LuukMay 12, 2014 at 10:36
-
Also, for TwoWay Binding on Settings see stackoverflow.com/a/845033/194717– TonyJan 8, 2017 at 15:18
-
This together with stackoverflow.com/questions/870893/… solved my problem. Thanks! One thing remains unclear: Why does my app run fine within Visual Studio despite this issue?– StewartDec 22, 2021 at 13:43