代码中的OneTime绑定与直接设置本地值不同吗?

本文关键字:设置 OneTime 绑定 代码 | 更新日期: 2023-09-27 18:18:00

这个OneTime绑定和

有什么区别?
//Create the source string
string s = "Hello";
//Create the binding description
Binding b = new Binding("");
b.Mode = BindingMode.OneTime;
b.Source = s;
//Attach the binding to the target
MyText.SetBinding(TextBlock.TextProperty, b);

这吗?

MyText.Text = s;

代码中的OneTime绑定与直接设置本地值不同吗?

非常不同。通过第二种方法,您可以在代码中的任何时间点更改绑定值。但对于一次性绑定,绑定值仅在应用启动或数据上下文更改时计算。