{"id":33624,"date":"2017-10-05T14:05:47","date_gmt":"2017-10-05T21:05:47","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=33624"},"modified":"2017-10-05T14:05:47","modified_gmt":"2017-10-05T21:05:47","slug":"databinding-power-moves-you-may-not-be-using-yet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/databinding-power-moves-you-may-not-be-using-yet\/","title":{"rendered":"DataBinding Power Moves You May Not Be Using (Yet)"},"content":{"rendered":"<p>\t\t\t\tDataBinding is a fantastic feature that cleanly decouples your UI from the backing data models while providing the plumbing to move data back and forth as you need. But, are you getting the most out of the Binding features in Xamarin.Forms? Building on our previous <a href=\"https:\/\/blog.xamarin.com\/introduction-to-data-binding\/\">Introduction to DataBinding<\/a> and <a href=\"https:\/\/blog.xamarin.com\/advanced-data-binding-for-ios-android-and-windows\/\">Advanced Data Binding<\/a> posts, I\u2019ll show you some of our most powerful features that you should be using.<\/p>\n<p><strong>Compiled Bindings in XAML<\/strong>\nCompiled is faster. It also means you get data type validation at compile time. The first requirement is to make sure you\u2019ve <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/xaml\/xamlc\/\">enabled XAML Compilation<\/a>. It\u2019s really easy. Add this assembly tag outside your application\u2019s Namespace once.<\/p>\n<pre class=\"EnlighterJSRAW\">using Xamarin.Forms.Xaml;\n...\n[assembly: XamlCompilation (XamlCompilationOptions.Compile)]\nnamespace ContosoAir\n{\n  ...\n}<\/pre>\n<p>Then, to benefit from compiled bindings, you need to specify the <code>x:DataType<\/code> in your XAML, typically at the highest level or the wrapping element of the children that you\u2019re binding. A good rule to keep in mind is to set the <code>X:DataType<\/code> at the same level where you set the <code>BindingContext<\/code>. Below is an example page that display a list of deals. It uses the MVVM pattern, so the current <code>BindingContext<\/code> of this page is a ViewModel with an <code>ObservableCollection&lt;Deal&gt;<\/code> called MyDeals.<\/p>\n<pre class=\"EnlighterJSRAW\">namespace ContosoAir.Clients.Models\n{\npublic class Deal\n{\npublic int Id { get; set; }\npublic string CityName { get { return ToName; } }\npublic string CityImage { get { return ToName; } }\npublic double Price { get; set; }\npublic string FromName { get; set; }\npublic string FromCode { get; set; }\npublic string DepartTime { get; set; }\npublic string ArrivalTime { get; set; }\npublic string Hours { get; set; }\npublic string ToName { get; set; }\npublic string ToCode { get; set; }\npublic int Stops { get; set; }\n}\n}\n<\/pre>\n<p>The model <code>Deal<\/code> is basic, and doesn\u2019t implement INotifyPropertyChanged. Below you see this collection bound to the <code>ListView<\/code> and then the <code>x:DataType<\/code> set on the <code>DataTemplate<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\">\n\n...\n\n...\n\n\n\n\n\n\n\n\n...\n\n<\/pre>\n<p>XAMLC will generate the code to access the properties, and hook (not in this case) to PropertyChanged.<\/p>\n<blockquote style=\"text-align: left;font-size: 12px;padding: 10px 20px;border-left: 3px solid blue\"><p>Note the <code>xmlns<\/code> reference supports the <code>using<\/code> instead of <code>clr-namespace<\/code> when in the same assembly.<\/p><\/blockquote>\n<p>Here is the <code>DealControl<\/code> referenced in the previous code showing that it\u2019s just a simple binding.<\/p>\n<pre class=\"EnlighterJSRAW\">\n\n\n...\n<Label \/>\n...\n\n\n<\/pre>\n<p>You may re-specify the DataType at any point in your view hierarchy.<\/p>\n<p><strong>OneWayToSource<\/strong>\nWhen you want your binding to be light as a feather, and only need to make sure your source is updated as the user makes changes, then use the <code>OneWayToSource<\/code> mode.<\/p>\n<p><a href=\"https:\/\/developer.xamarin.com\/api\/field\/Xamarin.Forms.BindingMode.OneWayToSource\/\">OneWayToSource<\/a> as defined in the API docs:<\/p>\n<blockquote style=\"text-align: left;font-size: 12px;padding: 10px 20px;border-left: 3px solid blue\"><p>Indicates that the binding should only propagates changes from target (the BindableObject) to source (usually the View Model). This is mainly used for read-only BindableProperty values.<\/p><\/blockquote>\n<p>This is useful for when you have input fields that are entry only and you want the values reflected on your ViewModel.<\/p>\n<pre class=\"EnlighterJSRAW\">\n<\/pre>\n<p>Because the source doesn\u2019t issue property changed events, it\u2019s very efficient. For an additional example, check out <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/xaml\/xaml-basics\/data_binding_basics\/#Backwards_Bindings\">this sample project<\/a> that shows binding one way between controls.<\/p>\n<p><strong>StringFormat<\/strong>\nYou have a lot of inline flexibility to format your content by leveraging <code>StringFormat<\/code> with bindings. Here are a few quick examples:<\/p>\n<pre class=\"EnlighterJSRAW\"><Label \/>\n\n<Label \/>\n\n<Label \/>\n<\/pre>\n<h2>Optimize Your Bindings<\/h2>\n<p>The latest version of Xamarin.Forms, 2.4.0, delivers the best XAML DataBinding performance to date. Update the NuGets in all your projects today and try out these tips.<\/p>\n<p>Remember to check out our previous articles on DataBinding, and our excellent documentation guides:<\/p>\n<ul>\n<li><a href=\"https:\/\/blog.xamarin.com\/introduction-to-data-binding\/\">Introduction to Data Binding<\/a><\/li>\n<li><a href=\"https:\/\/blog.xamarin.com\/advanced-data-binding-for-ios-android-and-windows\/\">Advanced Data Binding<\/a><\/li>\n<li><a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/xaml\/xaml-basics\/data_binding_basics\/\">Data Binding Basics<\/a><\/li>\n<li><a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/xaml\/xaml-basics\/data_bindings_to_mvvm\/\">From Data Bindings to MVVM<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/forums.xamarin.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Discuss this post on the Xamarin Forums<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DataBinding is a fantastic feature that cleanly decouples your UI from the backing data models while providing the plumbing to move data back and forth as you need. But, are you getting the most out of the Binding features in Xamarin.Forms? Building on our previous Introduction to DataBinding and Advanced Data Binding posts, I\u2019ll show [&hellip;]<\/p>\n","protected":false},"author":553,"featured_media":33635,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[16],"class_list":["post-33624","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>DataBinding is a fantastic feature that cleanly decouples your UI from the backing data models while providing the plumbing to move data back and forth as you need. But, are you getting the most out of the Binding features in Xamarin.Forms? Building on our previous Introduction to DataBinding and Advanced Data Binding posts, I\u2019ll show [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/33624","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\/553"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=33624"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/33624\/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=33624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=33624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=33624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}