Binding Control To Control In Xamarin.Forms

Introduction

Data binding is a mechanism in Xamarin Forms Applications. Binding is a common context. It is the process of mapping a property on a page, to a property in a view or ViewModel. In this article, I will show you how to bind Control property to Control property binding in Xamarin. Forms.xamarin

Create New Xamarin. Forms Application

Let's start with creating a new Xamarin Forms Project in Visual Studio.

Open Run - Type Devenev.Exe and enter - New Project (Ctrl+Shift+N) - select Blank XAML app (Xamarin.Forms Portable) template.

You can refer to my previous article to create new Xamarin forms Application at http://www.c-sharpcorner.com/article/how-to-create-first-xamarin-form-application/

xamarin

Binding control to control, using XAML

Data bindings can be defined to link properties of two controls on the same page. In this case, you set the BindingContext of the target object, using the x: Reference markup extension.

In the sample given below, add one Entry control and Label control to print the text.

  1. <StackLayout>  
  2.     <Entry x:Name="txtname"></Entry>  
  3.     <Label Text="Welcome Mr.." VerticalOptions="Center" HorizontalOptions="Center" />  
  4.     <Label BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text}" VerticalOptions="Center" HorizontalOptions="Center" /> </StackLayout> 

Entry Control Name : txtname.

Label control adding Binding Context and Binding the value.

While assigning binding Context, we can use two ways, which are given below.

  1. BindingContext="{x:Reference Name=txtname} OR BindingContext="{x:Reference txtname} // Name attribute is not required  

Similarly while assigning the value to property, we can use two ways given below.

  1. Text="{Binding Path=Text}" OR Text="{Binding Text}" // Path attribute is not required  

xamarin

Binding control with String Builder

All the database data or other storage data is not a formatted data, so you can bind property with the string or any type, as shown below. 

  1. <Label BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text, StringFormat='Welcome Mr {0}'}" />  
  2. <StackLayout>  
  3.     <Entry x:Name="txtname"></Entry>  
  4.     <Label BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text, StringFormat='Welcome Mr {0}'}" VerticalOptions="Center" HorizontalOptions="Center" /> </StackLayout>  

Binding control in C#

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

  1. label.SetBinding(Label.TextProperty, new Binding("<Property Name>", stringFormat: "Welcome Mr {0}", source: <BindingContext Control name>));    
  2.     
  3. or    
  4.     
  5. var label = new Label{ BindingContext = <Control Name> };    
  6. label.SetBinding(Label.TextProperty, new Binding("<Property Name>", stringFormat: "Welcome Mr {0}"));  
  7.   
  8. StackLayout layout = new StackLayout();  
  9. var editor = new Editor();  
  10. Children.Add(editor);  
  11. var label = new Label();  
  12. SetBinding(Label.TextProperty, new Binding("Text", stringFormat: "Welcome Mr {0}", source: editor));  
  13. Children.Add(label);  
  14. Content = layout;  

Binding User Control

Xamarin Forms doesn’t have a control called as User Control. However, we can make any VisualElement or a combination of Visual elements into a reusable control i.e. to use on different pages, just like a User Control.

Create User Control

Right click on PCL project > Select Forms XAML View and click Add.

xamarin

In XAML and C# file, change the content's view to any layout control like Grid, Stack layout

In XAML page, it is given below.

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <Grid xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MVVMXamarin.MyControl">  
  3.     <Label Text="{Binding Text, Source={x:Reference this}}" /> </Grid>  

In the code backend file, you might want to make your control, have a bindable property to pass the data through to it. On the each page, it is used. First, create a Bindable property in your UserControl. It doesn’t have to be a bindable property, if you just want to pass a static value through, however if you want to bind to it, you must make it bindable.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7. namespace MVVMXamarin {  
  8.     public partial class MyControl: Grid {  
  9.         public MyControl() {  
  10.             InitializeComponent();  
  11.         }  
  12.         public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(MyControl));  
  13.         public string Text {  
  14.             get {  
  15.                 return (string) GetValue(TextProperty);  
  16.             }  
  17.             set {  
  18.                 SetValue(TextProperty, value);  
  19.             }  
  20.         }  
  21.     }  
  22. }  

Going to your MainPage, you can now assign a value to the Text property. You can also use {Binding PropertyName}, if you want, as this is a Bindable property.

  1. <StackLayout>  
  2.     <Entry x:Name="txtname"></Entry>  
  3.     <local:MyControl BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text, StringFormat='Welcome Mr {0}'}"> </local:MyControl>  
  4. </StackLayout>  



Similar Articles