WPF 绑定双向不起作用,单向到源工作.为什么

本文关键字:工作 为什么 绑定 不起作用 WPF | 更新日期: 2023-09-27 17:57:23

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

控件的用法:

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

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

WPF 绑定双向不起作用,单向到源工作.为什么

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

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

由此:

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

对此:

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

现在它起作用了!

相关文章: