Win 8 exploration: Simple Databinding

This is another in my post series on WinRT programming for C# developers. For review, I’m blogging my notes on running and modifying each of the samples from this page: http://code.msdn.microsoft.com/windowsapps/Windows-8-Modern-Style-App-Samples

The next sample provides an overview of WinRT data binding.  This sample actually has seven different scenarios; it's one of the biggest samples so far.

However, I'm only going to briefly cover this sample.  TLDR Version:  If you've used DataBinding in WPF or Silverlight, it is very familiar.

For those interested in more:

You can support one way, two way, and one time bindings:

          <
          TextBox
          x:Name
          ="tbOneWayDataBound"
        
          Text
          ="{Binding ElementName=sliderOneWayDataSource, Path=Value, Mode=OneWay}"
        
          Grid.Column="2"Grid.Row="2"HorizontalAlignment="Left"VerticalAlignment="Top"Width="150"/>
          <
          TextBox
          x:Name
          ="tbTwoWayDataBound"
        
          Text
          ="{Binding ElementName=sliderTwoWayDataSource, Path=Value, Mode=TwoWay}"
        
          Grid.Column="2"Grid.Row="4"HorizontalAlignment="Left"VerticalAlignment="Top"Width="150"/>
          <
          TextBox
          x:Name
          ="tbOneTimeDataBound"
        
          Text
          ="{Binding ElementName=sliderOneTimeDataSource, Path=Value, Mode=OneTime}"
        
          Grid.Column="2"Grid.Row="6"HorizontalAlignment="Left"VerticalAlignment="Top"Width="150"/>

You can create Converters similar to WPF or Silverlight:

          <
          TextBox
          x:Name
          ="tbValueConverterDataBound"
        
          Text
          ="{Binding ElementName=sliderValueConverter, Path=Value, Mode=OneWay, Converter={StaticResource GradeConverter}}"
        
          Margin
          ="5"
          Width
          ="150"
          />
        

 

          public
          class S2Formatter : IValueConverter
{
          //Convert the slider value into Grades
        
          public
          object Convert(objectvalue, System.Type type, object parameter, string language)
    {
          int _value;
          string _grade = string.Empty;
          //try parsing the value to int
        
          if (Int32.TryParse(value.ToString(), out _value))
        {
          if (_value < 50)
                _grade = "F";
          else
          if (_value < 60)
                _grade = "D";
          else
          if (_value < 70)
                _grade = "C";
          else
          if (_value < 80)
                _grade = "B";
          else
          if (_value < 90)
                _grade = "A";
          else
          if (_value < 100)
                _grade = "A+";
          else
          if (_value == 100)
                _grade = "SUPER STAR!";
        }
          return _grade;
    }
          public
          object ConvertBack(objectvalue, System.Type type, object parameter, string language)
    {
          throw
          new NotImplementedException(); //doing one-way binding so this is not required.
    }
}

Other scenarios in this sample show how to respond to data source changes (It looks like INotifyPropertyChanged), bind to elements in a collection (just like in WPF or Silverlight) , bind to color properties (just like WPF and Silverlight), modify collections (like WPF and Silverlight), and navigate collections (like in WPF or Silverlight).

In short, it's really familiar code, if you've done anything with WPF or Silverlight.

Created: 4/6/2012 5:52:54 PM

Current Projects

I create content for .NET Core. My work appears in the .NET Core documentation site. I'm primarily responsible for the section that will help you learn C#.

All of these projects are Open Source (using the Creative Commons license for content, and the MIT license for code). If you would like to contribute, visit our GitHub Repository. Or, if you have questions, comments, or ideas for improvement, please create an issue for us.

I'm also the president of Humanitarian Toolbox. We build Open Source software that supports Humanitarian Disaster Relief efforts. We'd appreciate any help you can give to our projects. Look at our GitHub home page to see a list of our current projects. See what interests you, and dive in.

Or, if you have a group of volunteers, talk to us about hosting a codeathon event.