Search

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, April 17, 2017

Binding Control to Control in Xamarin Forms

Introduction:

Databinding is a mechanism in Xamarin Forms Application.  Binding is common context, is the process of mapping a property on a Page, to a property in a view or ViewModel. In this Article I will show how bind Control property to Control property  binding in xamarin Forms.
 
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 my previous article for create new xamarin forms application from here - http://www.c-sharpcorner.com/article/how-to-create-first-xamarin-form-application/

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 following sample added one Entry control and Label control for print text
  1. <StackLayout>
  2.  <Entry x:Name ="txtname"></Entry>
  3.  <Label  Text="Welcome Mr.."
  4.           VerticalOptions="Center"
  5.           HorizontalOptions="Center" />
  6.  <Label BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text}"
  7.           VerticalOptions="Center"
  8.           HorizontalOptions="Center" />
  9.  </StackLayout>
Entry Control Name : txtname
Label control adding Binding Context and Binding the value.
While assign binding Context, we can use following two way
BindingContext="{x:Reference Name=txtname} OR BindingContext="{x:Reference txtname} // Name attribute is not required
Same like while assign value to property, we can use following two way
Text="{Binding Path=Text}" OR Text="{Binding Text}" // Path attribute is not required

Binding Control with String Builder:
All the database data or other srorage data is not formatted data so you Can bind Property with string or any type like below  
<Label BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text, StringFormat='Welcome Mr {0}'}"/>
  1. <StackLayout>
  2. <Entry x:Name ="txtname"></Entry>
  3. <Label BindingContext="{x:Reference Name=txtname}" Text="{Binding Path=Text, StringFormat='Welcome Mr {0}'}"
      1. VerticalOptions="Center"
      2. HorizontalOptions="Center" />
  4. </StackLayout>
Binding Control in C#:
You can do it effectively the same way as the XAML example simply by setting your view's BindingContext or Source to the other view.
label.SetBinding(Label.TextProperty, new Binding("<Property Name>", stringFormat: "Welcome Mr {0}", source: <BindingContext Control  name>));
or
var label = new Label{ BindingContext = <Control Name> };
label.SetBinding(Label.TextProperty, new Binding("<Property Name>", stringFormat: "Welcome Mr {0}"));
  1. StackLayout layout = new StackLayout();
  2. var editor = new Editor();
  3. layout.Children.Add(editor);
  4. var label = new Label();
  5. label.SetBinding(Label.TextProperty, new Binding("Text", stringFormat: "Welcome Mr {0}", source: editor));
  6. layout.Children.Add(label);
  7. this.Content = layout;
Binding User Control:
Xamarin Forms doesn’t have a control called a User Control. However, we can make any VisualElement, or combination of Visual Element’s into a reusable control, to use on different pages, just like a User Control.
Create User Control:
Right Click PCL project > Select” Forms XAML View”  and click on Add

In xaml and C# file, change content View to any layout control like Grid, Stack layout

In Xaml Page:
  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}}" />
  4.  
  5. </Grid>
In Code behind file, you might want to make your control, have a bindable property to pass data through to it, on each page it is used. First, create a Bindable Property in your UserControl. It doesn’t have to be bindable 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. {
  9.    public partial class MyControl : Grid
  10.    {
  11.        public MyControl()
  12.        {
  13.            InitializeComponent();
  14.        }
  15.        public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(MyControl));
  16.        public string Text
  17.        {
  18.            get
  19.            {
  20.                return (string)GetValue(TextProperty);
  21.            }
  22.            set
  23.            {
  24.                SetValue(TextProperty, value);
  25.            }
  26.        }
  27.    }
  28. }
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}'}">
  4. </local:MyControl>
  5.  </StackLayout>


1 comment:

  1. It was fascinating and will be really helpful to me. Additionally, the information you have provided here is accurate and excellent.
    chatbot

    ReplyDelete