【问题标题】:A TwoWay or OneWayToSource binding cannot work on the read-only propertyTwoWay 或 OneWayToSource 绑定无法在只读属性上工作
【发布时间】:2010-10-10 01:39:40
【问题描述】:

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

【问题讨论】:

  • 如果您确实想要双向绑定,则属性设置器必须是公共的。见stackoverflow.com/questions/8773150/…
  • 而且从显而易见的部门来看,setter也必须存在;即只有一个 get 的属性会出现同样的问题。
  • @ColonelPanic “我有一个需要在文本框中显示的只读属性”告诉我提问者没有尝试进行双向绑定。我认为他没有指定绑定模式,所以它默认为 TwoWay。

标签: wpf binding


【解决方案1】:

没有代码很难猜到,但您应该可以将 BindingMode 设置为 OneWay。

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

或来自代码:

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

【讨论】:

猜你喜欢
  • 2012-02-05
  • 2019-02-08
  • 2019-11-21
  • 1970-01-01
  • 2010-10-14
  • 1970-01-01
  • 2015-03-14
  • 2011-04-04
相关资源
最近更新 更多