MOBILE

Windows Phone 7 Advanced Programming Model : Advanced Data Binding (part 4) - Data Bind to Anything

9/20/2011 3:44:53 PM

4. Data Bind to Anything

There are situations where you need to data bind to a value and the value type or format does not quite line up with what you need. The IValueConverter interface allows your application to data bind to just about anything including data that does not match the type expected for a particular control.

As an example, let's say you have a field that takes values of either Yes or No. You could use a ListPicker but a CheckBox makes more sense since it is just two values. You can implement IValueConverter to convert true to a Yes value and No value to false (and vice versa) while still using standard data binding mechanisms.

In this section, we create a custom IValueConverter named HtmlToImageUriConverter. This very simple converter parses the AppHub Feed html, previously displayed as raw html text, into a URL that points to an image related to the content. The URL is extracted from the HTML. Listing 4 shows the code for the converter.

Example 4. WebBrowserHTMLProp Code File
using System;
using System.Windows.Data;

namespace AdvancedDataBinding.Converters
{
public class HtmlToImageUriConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
string html = (string)value;
string imageUrl = "";
if (null != html)
{
string[] strings = html.Split('"');
if (strings.Length > 3)
imageUrl = strings[3].Trim();
}
return imageUrl;
}

public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}


The converter is somewhat brittle in that it depends on the URL value to be the fourth string when split, but one wouldn't expect the feed format to change very often, and it makes for an interesting sample. The converter is made available to the DatabindToAnything.xaml View via an xml namespace import:

xmlns:converters="clr-namespace:AdvancedDataBinding.Converters"

The converter is added as a resource in this XAML:

<phone:PhoneApplicationPage.Resources>
<converters:HtmlToImageUriConverter x:Key="HtmlToImageConverter"/>
</phone:PhoneApplicationPage.Resources>

Applying the converter is a simple modification in XAML or visually in Expression Blend. Here is the updated ListBoxDataTemplate:

<DataTemplate>
<StackPanel Margin="0,0,0,30">
<TextBlock Text="{Binding Title}" Margin="0,0,12,0" Style="{StaticResource PhoneTextLargeStyle}" />
<Image delay:LowProfileImageLoader.UriSource=
"{Binding Description, Converter={StaticResource HtmlToImageConverter}}" Margin="0,6,0,4" Width="100" HorizontalAlignment="Left" />
<HyperlinkButton NavigateUri="{Binding Link}" FontFamily="Segoe WP SemiLight" TargetName="_blank"
FontSize="18.667" FontStyle="Italic" Content="More..." HorizontalAlignment="Left" Margin="-12,0,0,0"/>
</StackPanel>
</DataTemplate>


The Binding.Converter parameter is configured to the added StaticResource to apply the converter for the Image object. Note that this sample also implements the LowProfileImageLoader for better performance as well as the PerformanceProgressBar to give better visual status to the user. Figure 6 shows the resulting output with a nice image instead of the raw HTML that was displayed previously.

Figure 6. AppHub feed with articles and corresponding images

In this section, data binding concepts are extended to more real world scenarios while also incorporating MVVM concepts. The next section provides a quick tour of available encryption services in Windows Phone 7.
Other  
 
Most View
Plain Text: Not Exactly Plain Sailing (Part 1)
Guide To Upgrades With The Greatest Effects (Part 2)
Compliance & The Cloud (Part 1)
Windows Server 2008 and Windows Vista : Creating Custom ADMX and ADML Files (part 1) - ADMX Schema , ADMX File Structure , ADML File Structure
Home Cinema Joy Can Be Yours - Just Seek Out Some Cheap Thrills (Part 1) : Pioneer VSX-922
Windows 7 : Protecting Your Data from Loss and Theft - NTFS File Permissions
Alternatives To Stay Photographically Connected
Windows Server 2003 : Supporting and Troubleshooting Terminal Server (part 2) - Managing and Troubleshooting Terminal Server - Points of Administration
Desktop Organisation Tools (Part 3) : RocketDock, Dexpot
Spotlight – Money Dashboard
Top 10
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 3) - Defining Block List Exceptions and Global Allow/Block Lists
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 2) - Filtering Connections with IP Block Lists
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 1) - Filtering Spam and Other Unwanted E-Mail by Sender, Filtering Spam and Other Unwanted E-Mail by Recipien
Microsoft Exchange Server 2010 : Creating and Managing Remote Domains (part 3) - Configuring Messaging Options for Remote Domains , Removing Remote Domains
Microsoft Exchange Server 2010 : Creating and Managing Remote Domains (part 2) - Creating Remote Domains