{"id":23827,"date":"2016-01-13T13:05:12","date_gmt":"2016-01-13T21:05:12","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=23827"},"modified":"2019-04-04T09:07:43","modified_gmt":"2019-04-04T16:07:43","slug":"introduction-to-data-binding","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/introduction-to-data-binding\/","title":{"rendered":"Introduction to Data Binding"},"content":{"rendered":"<p>\t\t\t\tWriting code that sets the properties of user interface controls to the required data is a tried-and-tested technique for wiring a user interface (UI) to a data source, but can result in having to write lengthy and repetitive code to ensure that the UI is correctly synchronized.<\/p>\n<p>Writing code to wire a user interface to a data source doesn&#8217;t have to feel like the dark ages! In this blog post, I\u2019m going to detail an alternative to manually connecting a UI to its data source. Data binding is a technique to automatically synchronize a user interface with its data source and can vastly simplify how your app displays and interacts with data.<\/p>\n<h2>Data Binding 101<\/h2>\n<p>Data binding connects two objects, called the <em>source<\/em> and the <em>target<\/em>. The <em>source<\/em> object provides the data. The <em>target<\/em> object, which must be a bindable property, will consume (and often display) data from the source object. For example, a <a href=\"https:\/\/developer.xamarin.com\/api\/type\/Xamarin.Forms.Label\/\" target=\"_blank\"><code>Label<\/code><\/a> (<em>target <\/em>object) will commonly bind its <a href=\"https:\/\/developer.xamarin.com\/api\/property\/Xamarin.Forms.Label.Text\/\" target=\"_blank\"><code>Text<\/code><\/a> property to a public string property in a source object. The following diagram illustrates the binding relationship:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-23865 size-full\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/data-binding@2x1.png\" alt=\"Displaying the relationship between the source and target objects in data binding.\" width=\"1076\" height=\"158\" \/><\/p>\n<p>The main benefit of data binding is that you no longer have to worry about synchronizing data between your views and data source. Changes in the source object are automatically pushed to the target object behind-the-scenes by the binding framework, and changes in the target object can be optionally pushed back to the source object.<\/p>\n<p>Establishing data binding is a two step process:<\/p>\n<ol>\n<li>The <a href=\"https:\/\/developer.xamarin.com\/api\/property\/Xamarin.Forms.BindableObject.BindingContext\/\" target=\"_blank\"><code>BindingContext<\/code><\/a> property of the <em>target<\/em> object must be set to the<em> source<\/em>.<\/li>\n<li>A binding must be established between the target and the source. In XAML, this is achieved by using the <a href=\"https:\/\/developer.xamarin.com\/api\/type\/Xamarin.Forms.Xaml.BindingExtension\/\" target=\"_blank\"><code>Binding<\/code><\/a> markup extension.<\/li>\n<\/ol>\n<p>The following code, taken from the <a href=\"https:\/\/github.com\/davidbritch\/xamarin-forms\/tree\/master\/DataBinding101\" target=\"_blank\">accompanying sample<\/a>, shows an example of performing data binding in XAML:<\/p>\n<pre class=\"lang:xml decode:true\" title=\"HomePage class\">&lt;ContentPage xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\" xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\" x:Class=\"DataBinding101.HomePage\"&gt;\n\t&lt;StackLayout Padding=\"0,20,0,0\"&gt;\n\t\t&lt;Label Text=\"Data Binding 101 Demo\" FontAttributes=\"Bold\" HorizontalOptions=\"Center\" \/&gt;\n\t\t&lt;Label Text=\"Forename:\" \/&gt;\n\t\t&lt;Entry Text=\"{Binding Forename, Mode=TwoWay}\" \/&gt;\n\t\t&lt;Label Text=\"Surname:\" \/&gt;\n\t\t&lt;Entry Text=\"{Binding Surname, Mode=TwoWay}\" \/&gt;\n\t\t&lt;StackLayout Padding=\"0,20,0,0\" Orientation=\"Horizontal\"&gt;\n\t\t\t&lt;Label Text=\"Your forename is:\" \/&gt;\n\t\t\t&lt;Label Text=\"{Binding Forename}\" \/&gt;\n\t\t&lt;\/StackLayout&gt;\n\t\t&lt;StackLayout Orientation=\"Horizontal\"&gt;\n\t\t\t&lt;Label Text=\"Your surname is:\" \/&gt;\n\t\t\t&lt;Label Text=\"{Binding Surname}\" \/&gt;\n\t\t&lt;\/StackLayout&gt;\n\t&lt;\/StackLayout&gt;\n&lt;\/ContentPage&gt;<\/pre>\n<p>Although the\u00a0<a href=\"https:\/\/developer.xamarin.com\/api\/property\/Xamarin.Forms.BindableObject.BindingContext\/\" target=\"_blank\"><code>BindingContext<\/code><\/a>\u00a0property of the <strong><code>HomePage<\/code><\/strong> class (the target object) can be set in XAML, here it&#8217;s set in code-behind to <strong><code>DetailsViewModel<\/code><\/strong> (the source object):<\/p>\n<pre class=\"lang:c# decode:true\" title=\"HomePage code-behind\">public partial class HomePage : ContentPage\n{\n\tpublic HomePage ()\n\t{\n\t\tInitializeComponent ();\n\t\tBindingContext = new DetailsViewModel ();\n\t}\n}<\/pre>\n<p>While the <a href=\"https:\/\/developer.xamarin.com\/api\/property\/Xamarin.Forms.BindableObject.BindingContext\/\" target=\"_blank\"><code>BindingContext<\/code><\/a> property of each target object can be individually set, this isn\u2019t necessary. <strong><code>BindingContext<\/code><\/strong> is a special property that\u2019s inherited by all its children. Therefore, when the <strong><code>BindingContext<\/code><\/strong> on the <a href=\"https:\/\/developer.xamarin.com\/api\/type\/Xamarin.Forms.ContentPage\/\" target=\"_blank\"><code>ContentPage<\/code><\/a> is set to <strong><code>DetailsViewModel<\/code><\/strong>, all of the children of the <strong><code>ContentPage<\/code><\/strong> have the same <strong><code>BindingContext<\/code><\/strong>, and can bind to public properties of <strong><code>DetailsViewModel<\/code><\/strong>.\u00a0For information about setting the\u00a0<strong><code>BindingContext<\/code><\/strong>\u00a0in XAML, see\u00a0<a href=\"http:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/xaml-basics\/data_bindings_to_mvvm\/\" target=\"_blank\">From Data Bindings to MVVM<\/a>.<\/p>\n<p>The <a href=\"https:\/\/developer.xamarin.com\/api\/type\/Xamarin.Forms.Xaml.BindingExtension\/\" target=\"_blank\"><code>Binding<\/code><\/a> markup extension can specify several properties, including <a href=\"https:\/\/developer.xamarin.com\/api\/property\/Xamarin.Forms.Xaml.BindingExtension.Path\/\" target=\"_blank\"><code>Path<\/code><\/a> and <a href=\"https:\/\/developer.xamarin.com\/api\/property\/Xamarin.Forms.Xaml.BindingExtension.Mode\/\" target=\"_blank\"><code>Mode<\/code><\/a>. The <strong><code>Path<\/code><\/strong> property is used to specify the property name of the source object to use for the binding. However, this property name can be omitted provided that it\u2019s the first item in the <strong> <code>Binding<\/code><\/strong> markup extension.<\/p>\n<p>The <a href=\"https:\/\/developer.xamarin.com\/api\/property\/Xamarin.Forms.Xaml.BindingExtension.Mode\/\" target=\"_blank\"><code>Mode<\/code><\/a> property is used to specify the direction in which property value changes will propagate:<\/p>\n<ul>\n<li>A <strong><code>OneWay<\/code><\/strong> binding propagates changes from the <em>source<\/em> to the <em>target<\/em>.<\/li>\n<li>A <strong><code>TwoWay<\/code><\/strong> binding propagates changes in both directions, ensuring that the <em>source<\/em> and <em>target<\/em> objects are always synchronized.<\/li>\n<li>A <strong><code>OneWayToSource<\/code><\/strong> binding propagates changes from the <em>target<\/em> to the <em>source<\/em>, and is mainly used for read-only bindable properties.<\/li>\n<\/ul>\n<p>In Xamarin.Forms, the <a href=\"https:\/\/developer.xamarin.com\/api\/property\/Xamarin.Forms.Xaml.BindingExtension.Mode\/\" target=\"_blank\"><code>Mode<\/code><\/a> property defaults to <strong><code>OneWay<\/code><\/strong>, and can be omitted unless a different <a href=\"https:\/\/developer.xamarin.com\/api\/type\/Xamarin.Forms.BindingMode\/\" target=\"_blank\"><code>BindingMode<\/code><\/a> is required.<\/p>\n<h2>Property Change Notification<\/h2>\n<p>By default, the target object only receives the value of the source object when the binding is created. To keep the UI synchronized with the data source, we need a way to notify the target object when the source object has changed. This mechanism is provided by the <strong><code>INotifyPropertyChanged<\/code><\/strong> interface. Implementing this interface will provide notifications to any data-bound controls when the underlying property value changes. The following code example shows the source <strong><code>DetailsViewModel<\/code><\/strong> class, which is data bound from XAML, and how it implements the <strong><code>INotifyPropertyChanged<\/code><\/strong> interface:<\/p>\n<pre class=\"lang:c# decode:true \" title=\"DetailsViewModel class\">public class DetailsViewModel : INotifyPropertyChanged\n{\n\tstring forename, surname;\n\n\tpublic string Forename { \n\t\tget {\n\t\t\treturn forename;\n\t\t}\n\t\tset {\n\t\t\tif (forename != value) {\n\t\t\t\tforename = value;\n\t\t\t\tOnPropertyChanged (\"Forename\");\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic string Surname { \n\t\tget {\n\t\t\treturn surname;\n\t\t}\n\t\tset {\n\t\t\tif (surname != value) {\n\t\t\t\tsurname = value;\n\t\t\t\tOnPropertyChanged (\"Surname\");\n\t\t\t}\t\t\n\t\t}\n\t}\n\n\tpublic event PropertyChangedEventHandler PropertyChanged;\n\n\tprotected virtual void OnPropertyChanged (string propertyName)\n\t{\n\t\tvar changed = PropertyChanged;\n\t\tif (changed != null) {\n\t\t\tPropertyChanged (this, new PropertyChangedEventArgs (propertyName));\n\t\t}\n\t}\n}<\/pre>\n<p>The class fires a <code>PropertyChanged<\/code> event whenever the <code>Forename<\/code> or <code>Surname<\/code> properties change. Therefore, in the <a href=\"https:\/\/github.com\/davidbritch\/xamarin-forms\/tree\/master\/DataBinding101\" target=\"_blank\">sample application<\/a>, as the user enters their name, it\u2019s simultaneously displayed by the two <a href=\"https:\/\/developer.xamarin.com\/api\/type\/Xamarin.Forms.Label\/\" target=\"_blank\"><code>Label<\/code><\/a> instances, as shown in the following screenshots:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-23833 size-full\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/data-binding-101-screenshots.png\" alt=\"data-binding-101-screenshots\" width=\"1280\" height=\"768\" \/><\/p>\n<p>Note that as a best practice to avoid unnecessary events being fired, the\u00a0<code>PropertyChanged<\/code> event isn&#8217;t raised if the property value does not change.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>Data binding is used to synchronize a UI with its data source, and simplifies how a Xamarin.Forms application displays and interacts with its data. Provided that the source object implements the <strong><code>INotifyPropertyChanged<\/code><\/strong> interface, changes in the <em>source<\/em> object are automatically pushed to the <em>target<\/em> object by the binding framework, and changes in the <em>target<\/em> object can be optionally pushed to the <em>source<\/em> object.<\/p>\n<p>For more information about data binding, see <a href=\"http:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/xaml-basics\/data_binding_basics\/\" target=\"_blank\">Data Binding Basics<\/a>. For more information about the <strong>INotifyPropertyChanged<\/strong> interface, see <a href=\"http:\/\/developer.xamarin.com\/guides\/xamarin-forms\/user-interface\/xaml-basics\/data_bindings_to_mvvm\/\" target=\"_blank\">From Data Bindings to MVVM<\/a>. <a href=\"https:\/\/university.xamarin.com\/\" target=\"_blank\">Xamarin University<\/a> also provides a class on data binding in Xamarin.Forms. Ready to take your data binding skills to the next level? Check out the next post in our data binding series, where you will <a href=\"https:\/\/blog.xamarin.com\/advanced-data-binding-for-ios-android-and-windows\/\">learn advanced techniques like control-to-control bindings and converters<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Writing code that sets the properties of user interface controls to the required data is a tried-and-tested technique for wiring a user interface (UI) to a data source, but can result in having to write lengthy and repetitive code to ensure that the UI is correctly synchronized. Writing code to wire a user interface to [&hellip;]<\/p>\n","protected":false},"author":543,"featured_media":23833,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4,16],"class_list":["post-23827","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>Writing code that sets the properties of user interface controls to the required data is a tried-and-tested technique for wiring a user interface (UI) to a data source, but can result in having to write lengthy and repetitive code to ensure that the UI is correctly synchronized. Writing code to wire a user interface to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/23827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/543"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=23827"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/23827\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=23827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=23827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=23827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}