【问题标题】:WPF Binding TwoWay not working, OneWayToSource working. WHY?WPF 绑定 TwoWay 不工作,OneWayToSource 工作。为什么?
【发布时间】:2016-07-15 20:20:04
【问题描述】:

我有一个 TimeSpan 类型的 DependencyProperty 控件。当我尝试绑定到该属性时,该值没有被更新。

控件的使用:

<controls:TimeControl Time={Binding SomeTimeSpanProperty} />

当我更改控件中的时间值时,更改不会在 SomeTimeSpanProperty 中更新。但是,如果我将 {Binding SomeTimeSpanProperty} 更改为 {Binding SomeTimeSpanProperty,Mode=OneWayToSource},它就会更新。

【问题讨论】:

  • 为什么会有这么多的downwotes?这个问题到底有什么问题?

标签: c# wpf mvvm binding two-way


【解决方案1】:

我找到了解决方案。如果以后有人读到这篇文章想知道它是什么:

我必须将绑定模式显式设置为 TwoWay,因为 TimeSpan 类型属性的默认绑定模式是 OneWay。

从此:

<controls:TimeControl Time={Binding SomeTimeSpanProperty} />

到这里:

<controls:TimeControl Time={Binding SomeTimeSpanProperty,Mode=TwoWay} />

现在它工作了

【讨论】:

  • 在 WPF 中,Binding.Mode 属性的默认值为BindingMode.Default。这听起来可能很有趣,但这意味着 Binding " 使用绑定目标的默认 Mode 值。每个依赖属性的默认值都不同。通常,用户可编辑的控件属性,例如文本框和复选框,默认为双向绑定,而大多数其他属性默认为单向绑定。"
猜你喜欢
  • 2010-10-10
  • 1970-01-01
  • 2012-02-05
  • 2019-11-21
  • 2011-08-10
  • 2013-04-12
  • 2019-02-08
  • 2011-12-30
相关资源
最近更新 更多