【发布时间】:2016-08-31 07:14:04
【问题描述】:
Xamarin 网站有一个 section on View-To-View Binding,但示例是在 XAML 中,使用 x:Reference 标记扩展。如何在 C# 代码中做同样的事情?
【问题讨论】:
标签: c# mvvm data-binding xamarin xamarin.forms
Xamarin 网站有一个 section on View-To-View Binding,但示例是在 XAML 中,使用 x:Reference 标记扩展。如何在 C# 代码中做同样的事情?
【问题讨论】:
标签: c# mvvm data-binding xamarin xamarin.forms
只需将视图的 BindingContext 设置为另一个视图,您就可以像 XAML 示例一样有效地执行此操作。
例子:
var stepper = new Stepper();
var label = new Label {
BindingContext = stepper
};
label.SetBinding (Label.TextProperty, new Binding ("Value", stringFormat: "{0:F0}"));
或者,您可以设置 Binding.Source:
var stepper = new Stepper();
var label = new Label();
label.SetBinding (Label.TextProperty, new Binding ("Value", stringFormat: "{0:F0}", source: stepper));
【讨论】:
x:Reference 只是将一个对象的引用设置为另一个对象的BindingContext。
这基本上是:
myLabel.BindingContext = mySlider;
【讨论】: