0% found this document useful (0 votes)
2 views5 pages

Bindings

The document describes two methods of data binding in a Xamarin application using a 'Person' class. The first method demonstrates normal binding by setting individual bindings for each property, while the second method utilizes context binding by setting the BindingContext for the entire page. Both methods aim to display the properties of a 'Person' object in the user interface when a button is clicked.

Uploaded by

Hammad Bhatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Bindings

The document describes two methods of data binding in a Xamarin application using a 'Person' class. The first method demonstrates normal binding by setting individual bindings for each property, while the second method utilizes context binding by setting the BindingContext for the entire page. Both methods aim to display the properties of a 'Person' object in the user interface when a button is clicked.

Uploaded by

Hammad Bhatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Normal binding

Person.cs
public class Person
{
public string Name { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
}

mainPage.xaml
<ContentPage
x:Class="BindingDemo.MainPage">
<VerticalStackLayout
Padding="30,0"
Spacing="25"
VerticalOptions="Center">

<Label x:Name="txtName"
HorizontalOptions="Center"
Text="Usama"
FontSize="50"
VerticalOptions="Center"/>

<Button
x:Name="CounterBtn"
Text="Click me"
Clicked="OnCounterClicked"
HorizontalOptions="Fill" />
</VerticalStackLayout>

MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}

private void OnCounterClicked(object sender, EventArgs e)


{
var person = new Person()
{
Name = "Hammad",
Phone = "0009",
Address = "Usman hostel"

};
Binding personBinding = new Binding();
personBinding.Source = person;

personBinding.Path = "Name";
txtName.SetBinding(Label.TextProperty, personBinding);

}
}
Context binding

Person.cs
public class Person
{
public string Name { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
}

mainPage.xaml
<ContentPage x:Class="BindingDemo.MainPage">
<VerticalStackLayout
Padding="30,0"
Spacing="25"
VerticalOptions="Center">

<Label
// for method 1 this line is needed
// x:Name="txtName"
HorizontalOptions="Center"
Text="{Binding Name}"
FontSize="50"
VerticalOptions="Center"/>

<Label
// for method 1 this line is needed
// x:Name="txtPhone"
HorizontalOptions="Center"
Text="{Binding Phone}"
FontSize="50"
VerticalOptions="Center"/>

<Label
// for method 1 this line is needed
//x:Name="txtAddress"
HorizontalOptions="Center"
Text="{Binding Address}"
FontSize="50"
VerticalOptions="Center"/>

<Button
x:Name="CounterBtn"
Text="Click me"
Clicked="OnCounterClicked"
HorizontalOptions="Fill" />
</VerticalStackLayout>

MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void OnCounterClicked(object sender, EventArgs e)
{
var person = new Person()
{
Name = "Hammad",
Phone = "0009",
Address = "Usman hostel"

};
// method 1
// txtName.BindingContext = person;
// txtName.SetBinding(Label.TextProperty, "Name");

// txtPhone.BindingContext = person;
// txtPhone.SetBinding(Label.TextProperty, "Phone");

// txtAddress.BindingContext = person;
// txtAddress.SetBinding(Label.TextProperty, "Address");
//end here

// method 2
BindingContext=person;
}
}

You might also like