jbyrd
jbyrd

Reputation: 5585

Xamarin.Forms - View-To-View Bindings In C# Code?

The Xamarin website has a section on View-To-View Binding, but the examples are in XAML, using the x:Reference markup extension. How can I do the same thing in C# code?

Upvotes: 2

Views: 783

Answers (2)

Sven-Michael Stübe
Sven-Michael Stübe

Reputation: 14750

x:Reference is just setting a reference to an object as BindingContext of another object.

This is basically:

myLabel.BindingContext = mySlider;

Upvotes: 2

Dakshal Raijada
Dakshal Raijada

Reputation: 1261

You can do it effectively the same way as the XAML example simply by setting your view's BindingContext to the other view.

Example:

var stepper = new Stepper();

var label = new Label {
    BindingContext = stepper
};
label.SetBinding (Label.TextProperty, new Binding ("Value", stringFormat: "{0:F0}"));

Alternatively, you can set the Binding.Source:

var stepper = new Stepper();

var label = new Label();
label.SetBinding (Label.TextProperty, new Binding ("Value", stringFormat: "{0:F0}", source: stepper));

Source

Upvotes: 3

Related Questions