Windows Phone From Scratch #5 – Data Binding

Databinding is a somewhat advanced topic, typically covered in the second or even thedatabinding final third of most Silverlight books.  We’re going to tackle it in the next couple mini-tutorials because

  • It isn’t difficult to understand
  • It isn’t difficult to implement
  • It is critical both to most applications and to the principal design pattern used with Windows Phone: MVVM

    To cover this well, we’ll create the form shown in the illustration and we’ll populate that form by binding to an objects; specifically a person.  We’ll do this in four mini-tutorials. 
    1. Create the Person class
    2. Create the User Interface with layout controls and input controls
    3. Bind properties on the Person class to controls
    4. Advanced Binding, including binding from one UI element to another, and Binding conversions.

 

Create the Person class

To get started, create a new Windows Phone application in Visual Studio, and name it DataBinding.  Right click on the project and select Add->New->Class and name the class Person.cs. 

The class consists of an enumeration and a set of automatic properties,

public class Person
{
   public enum Sex
   {
      Male,
      Female,
   }
   public string Name { get; set; }
   public bool Moustache { get; set; }
   public bool Goatee { get; set; }
   public bool Beard { get; set; }
   public Sex WhichSex { get; set; }
   public double Height { get; set; }
   public DateTime BirthDate { get; set; }
   public bool Favorite { get; set; }

}

 

You can see pretty quickly how these properties will map to the various input controls shown in the illustration at the top.  The booleans can be either CheckBoxes or RadioButtons (depending on whether they are mutually exclusive or not). 

We are using a couple input controls you may not have seen yet, however.  We’re using a DatePicker for the birth date and a slider (with a TextBlock) for the height. More on these in the next tutorial. 

Before reading the next mini-tutorial you might want to try creating a first iteration of the UI yourself, based on what you already know, the illustration and the class definition.

Share

About Jesse Liberty

Jesse Liberty is a Master Consultant for Falafel Software, and has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Technical Evangelist for Telerik and for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS.
This entry was posted in Essentials, iPhoneToWP7, Patterns & Skills, WindowsPhone and tagged , . Bookmark the permalink.

5 Responses to Windows Phone From Scratch #5 – Data Binding

  1. bill burrows says:

    I am finding these mini-tutorials just right for me (maybe this specific one was a bit short). I am fairly familiar with Silverlight and XAML and am just wanting to apply what I know to the WP7 framework.

  2. @Bogdan
    Bogdan and Dennis….

    I have an on-going tutorial series (each entry is about 25 printed pages) on Windows Phone Programming. The request from the community was for another set of tutorials, from scratch, in bite-sized bites. Please bear with me for a few weeks and we’ll see if we can’t find a middle ground of something a bit longer but still in the mini-tutorial range.

    Thanks for the feedback, it is very much appreciated.

  3. Bogdan says:

    I too feel that these mini-tutorials are too short. They get you super excited reading the title of the RSS feed but leave you hungry 2 minutes later.

  4. ArchieCoder says:

    Hi,

    I’m developing an application and I use standard binding nothing fancy, but I’m facing a problem concerning how to handle validation.

    I would be very happy if you can provide an example on how to handle/display an error when the user will enter “0.43$%6″ for the Height in your example (if it is used in a TextBox.

    Thank you
    ArchieCoder

    PS : I own the C# book from you, it is a really good book!!

  5. Denis says:

    Sorry for the negative feedback but this article is about nothing actually. When reading it I can’t stop thinking that it was intended just to be indexed within search engines with WP7 tag rather than provide any value for the community.
    I would recommend posting the entire article instead of splitting it by passages.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>