Binding Formatting

Windows Phone Tutorial

When you are binding data there are additional properties that allow youBindingFormatting to format the display and to handle errors and null values.

The StringFormat property allows you to add any standard .NET format string that matches the type you are binding to.

The FallbackValue instructs the binding on what to display if the binding fails.

The TargetNullValue instructs the binding on what to display if the value bound to is null.

To see this at work, let’s create a very simple application with three StackPanels, each holding a prompt and a TextBlock,

<StackPanel Grid.Row="0"
   Orientation="Horizontal">
   <TextBlock
      Text="Promised Date: " />
   <TextBlock
      Margin="5,0,0,0"
      Text="{Binding PromisedDate, 
      StringFormat=d,
      FallbackValue='Date Not Found', 
      TargetNullValue='No Date'}" />
   </StackPanel>
<StackPanel
   Grid.Row="1"
   Orientation="Horizontal">
   <TextBlock
      Text="Delivered Date: " />
   <TextBlock
      Margin="5,0,0,0"
      Text="{Binding DeliveredDate, 
      StringFormat=d,
      FallbackValue='Date Not Found', 
      TargetNullValue='No Date'}" />
</StackPanel>
<StackPanel
   Grid.Row="2"
   Orientation="Horizontal">
   <TextBlock
      Text="Action: " />
      <TextBlock
         Margin="5,0,0,0"
      Text="{Binding Path=ReturnDate,
      StringFormat=d 
      FallbackValue='Date Not Found', 
      TargetNullValue='No Date'}" />
</StackPanel>

 

Notice that all three TextBlocks use the identical properties except for the name of the property that it will bind to.

In Main.xaml.cs we’ll create a tiny class to act as the data context for the binding,

public partial class MainPage : PhoneApplicationPage
{
    public class Book
    {
        public DateTime? PromisedDate
        {
            get { return DateTime.Now; }
            set { PromisedDate = value; }
        }

        public DateTime? ReturnDate
        {
            get { return null; }
        }
    }

 

Notice that PromisedDate returns a date, there is no property for DeliveredDate. amd ReturnDate returns null.  In the body of Main we’ll instantiate a Book object and set as the page’s DataContext. 

public MainPage()
{
    Book b = new Book();
    DataContext = b;
    InitializeComponent();
}

 

When this is run, PromisedData provides a date which is formatted by the StringFormat property.  Because there is no DeliveredDate the binding on that property fails and the FallbackValue is used.  Finally, because ReturnDate is null, the TargetNullValue is used for that value.  The output is shown in the illustration at the start of this posting.

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 online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and 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 Microsoft MVP.
This entry was posted in Data, Essentials, Mango, Mini-Tutorial, WindowsPhone and tagged . Bookmark the permalink.

5 Responses to Binding Formatting

  1. Oh my goodness! a tremendous article dude. Thanks Nevertheless

    I am experiencing problem with ur rss . Don’t know why Unable

    to subscribe to it. Is there anyone getting identical rss

    downside? Anyone who knows kindly respond. Thnkxxx

  2. I know this if off topic but I’m looking into starting my own weblog and was wondering what all is

    needed to get setup? I’m assuming having a blog like yours
    would cost a pretty penny? I’m not very

    web savvy so I’m not 100% certain. Any recommendations or advice

    would be greatly appreciated. Appreciate it

  3. hello there and thank you for your information – I have certainly picked up

    something new from right here. I did however expertise some technical
    points using this website, since I experienced
    to reload the web site many times previous

    to I could get it to load correctly. I had been wondering if your web host is OK?
    Not that

    I am complaining, but sluggish loading instances times will often affect your

    placement in google and can damage your quality score if
    ads and

    marketing with Adwords. Anyway I am adding this RSS to my e-mail and can look
    out for

    much more of your respective fascinating content.
    Ensure that you update

    this again very soon..

    Also visit my web-site – familydentalcentre.com

  4. The setter in PromisedDate will cause an infinite loop.

Comments are closed.