wpf TwoWay 或 OneWayToSource 绑定不能在只读属性上工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/590269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:04:49  来源:igfitidea点击:

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

wpfbinding

提问by Tony Trembath-Drake

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...

我有一个只读属性,需要在文本框中显示,并在运行时收到此错误。我已经设置IsEnabled="False"IsReadOnly="True"- 没有运气。其他搜索说 readonly 应该修复它,但不适合我。通过添加一个虚拟二传手,我有一个丑陋的解决方法......

回答by Razzie

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

没有代码很难猜测,但您应该能够将 BindingMode 设置为 OneWay。

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

or from code:

或从代码:

Binding binding = new Binding();
binding.Mode = BindingMode.OneWay;