C# 代码中的一次性绑定与直接设置本地值不同吗?
此一次性绑定之间有什么区别:C# 代码中的一次性绑定与直接设置本地值不同吗?,c#,silverlight,windows-phone-7,windows-phone-8,C#,Silverlight,Windows Phone 7,Windows Phone 8,此一次性绑定之间有什么区别: //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);
//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;
这很不一样。通过第二种方法,您可以在代码中的任何时间点更改绑定值。但使用一次性绑定时,绑定值仅在应用程序启动或数据上下文更改时计算。
请参阅一次性绑定说明。但在字符串绑定的情况下,绑定值是不可变的,因此没有区别,对吗?绑定仅在与通知其值更改的属性一起使用时才有用,从而提供了一种简单的机制,使模型与视图结合起来。在您的案例中,您正在使用一个变量进行绑定。所以,即使您更改了变量的值,在您通知它之前,它也不会反映在视图元素中。这对于字符串或任何其他类型都是正确的。好的,那么我们可以说在我的问题中这两种方法之间没有区别?