Advanced Databinding Part 4 – Binding .

In this post we will examine one of the aspects of advanced databinding that many people find confusing. Binding . (that is, Binding dot). However, it is surprisingly easy to explain.

Binding . allows you to access the entire binding context.

This is best understood with an example

<StackLayout BindingContext="{x:Static sys:DateTime.Now}"
                 HorizontalOptions="Center"
                 VerticalOptions="Center">

            <Label Text="{Binding Year, StringFormat='The year is {0}'}" />
            <Label Text="{Binding StringFormat='The month is {0:MMMM}'}" />
            <Label Text="{Binding Day, StringFormat='The day is {0}'}" />
            <Label Text="{Binding StringFormat='The time is {0:T}'}" />
            <Label Text="{Binding ., StringFormat='The full date is {0}'}" />
            <Label Text="{Binding}" />

        </StackLayout>

Here the stacklayout sets its BindingContext to DateTime.Now. That will allow us to bind to properties of the DateTime object that has the current local time. The first few lines are straight forward, we bind to properties of the DateTime object. For example,



<Label Text="{Binding Year, StringFormat='The year is {0}'}"

Here we bind to the Year property of the DateTime object. We then display that year in the label. The same is true for the next few lines. But DateTime does not have a property that provides us the entire date. We note, however, that this StackLayout (the parent element) has set the BindingContext to the DateTime object. We can therefore use
Binding . (binding dot) to access that BindingContext which will give us the full date.



<Label Text="{Binding ., StringFormat='The full date is {0}'}" />

The final line demonstrates that Binding . is really shorthand for Binding — and thus it too presents the full date/time.

Here’s the entire example:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:sys="clr-namespace:System;assembly=netstandard"
             x:Class="BindingDot.MainPage">

    <StackLayout>
        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <Label Text="Binding ." HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
        </Frame>
        <StackLayout BindingContext="{x:Static sys:DateTime.Now}"
                 HorizontalOptions="Center"
                 VerticalOptions="Center">

            <Label Text="{Binding Year, StringFormat='The year is {0}'}" />
            <Label Text="{Binding StringFormat='The month is {0:MMMM}'}" />
            <Label Text="{Binding Day, StringFormat='The day is {0}'}" />
            <Label Text="{Binding StringFormat='The time is {0:T}'}" />
            <Label Text="{Binding ., StringFormat='The full date is {0}'}" />
            <Label Text="{Binding}" />

        </StackLayout>
    </StackLayout>
</ContentPage>

Source Code here
First in this series here

Rodrigo Juarez is a full-stack and Xamarin Certified Mobile Professional developer. His mission is to solve complex problems for his clients focusing on the results, applying the most adequate technology available and best practices to ensure a cost-effective and high-quality solution. He has 20+ years of experience in a wide variety of projects in the development of applications for web, desktop and mobile using Microsoft technologies in areas such as management, services, insurance, pharmacy and banks.  He can be reached at  Rodrigo Juarez

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Essentials. Bookmark the permalink.