Windows Phone HubTile in depth| Part2: Data Binding
published on: 8/24/2011 | Views: N/A | Tags: WP7Toolkit Mango Binding
by WindowsPhoneGeek
This is the second article about the new HubTile control from the latest release of Windows Phone Toolkit - August 2011 (7.1 SDK). This time I am going to talk about data binding and using HubTile in more complex scenarios.
NOTE: In Part1 we talked about key properties, methods, events and the main features of the Windows Phone HubTile control. You can take a look at it for reference.
- Windows Phone HubTile in depth| Part1: key concepts and API
- Windows Phone HubTile in depth| Part2: Data Binding
- Windows Phone HubTile in depth| Part3: Freezing and Unfreezing tiles
Here is how the final data binding example should look like:
To begin with lets first create a new Windows Phone 7.1 application project and add a reference to the Microsoft.Phone.Controls.Toolkit.dll assembly in your Windows Phone application project.
NOTE: The Microsoft.Phone.Controls.Toolkit.dll assembly is installed with the toolkit and you can find it in:
For 32-bit systems:
C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Toolkit\Aug11\Bin\Microsoft.Phone.Controls.Toolkit.dll
For 64-bit systems:
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Toolkit\Aug11\Bin\Microsoft.Phone.Controls.Toolkit.dll
Alternatively you can select it directly from the "...\Silverlight for Windows Phone Toolkit Source & Sample - Aug 2011\Bin\" if you have downloaded the "Silverlight for Windows Phone Toolkit Source & Sample - Aug 2011.zip" instead.
Databinding HubTile Step by Step
This example demonstrates how to populate the HubTile control with data using data binding. We will implement a sample animated menu for a fast food company which shows: Title, Description, Price, etc.
- Defining the Data Source
Step1. Define the business/data class:
The first step is to define the data class. Lets create a "TileItem" class which exposes the following properties:
public class TileItem
{
public string ImageUri
{
get;
set;
}
public string Title
{
get;
set;
}
public string Notification
{
get;
set;
}
public bool DisplayNotification
{
get
{
return !string.IsNullOrEmpty(this.Notification);
}
}
public string Message
{
get;
set;
}
public string GroupTag
{
get;
set;
}
}
Step2. Create a new Images folder and add some images which will be shown in the HubTiles:
Step3. Create a sample collection with items of type TileItem:
public MainPage()
{
InitializeComponent();
List<TileItem> tileItems = new List<TileItem>()
{
new TileItem() {
ImageUri ="/Images/chicken.jpg",
Title = "Chicken", Notification = "$3.49", GroupTag = "TileGroup"
},
new TileItem() {
ImageUri ="/Images/wings.jpg",
Title = "Wings", Notification = "Only $2.49", GroupTag = "TileGroup"
},
new TileItem() {
ImageUri = "/Images/bonfillet.jpg",
Title = "Chicken\nFillet",
Message = "A couple of these will not hurt your diet."
},
new TileItem() {
ImageUri = "/Images/burger.jpg",
Title = "Burger", Notification = "$3.99"
},
new TileItem() {
ImageUri = "/Images/bucket.jpg",
Title = "Chicken\nBucket", Notification = "$19.99"
},
new TileItem() {
ImageUri = "/Images/fries.jpg",
Title = "Fries", Notification = "Only $1.99"
},
new TileItem() {
ImageUri = "/Images/caesar.jpg",
Title = "Caesar Salad", Notification = "$4.99"
},
new TileItem() {
ImageUri = "/Images/corn.jpg",
Title = "Corn", Notification = "Only $1.99"
},
};
//...
}
-
Databind HubTile
Step1. Define HubTile in XAML
We will add a ListBox which will be used to display a collection of TileItems using a HubTile control for each. The HubTile and its binding to TileItem properties is defined in the the ItemTemplate. We will also change the ItemsPanel of the list box to WrapPanel so that its items will be rendered appropriately. Here is how the code should look like:
<ListBox Grid.Row="0" x:Name="tileList">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<toolkit:HubTile Title="{Binding Title}" Margin="3"
Notification="{Binding Notification}"
DisplayNotification="{Binding DisplayNotification}"
Message="{Binding Message}"
GroupTag="{Binding GroupTag}"
Source="{Binding ImageUri}">
</toolkit:HubTile>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Step2. Populate the ListBox with data through its ItemsSource property:
public MainPage()
{
InitializeComponent();
//...
this.tileList.ItemsSource = tileItems;
}
Step3. Build the project and run it. Here is how the result should look like:
NOTE: The HubTile background color is determined by the PhoneAccentBrush. I.e. if your phone/emulator uses the Red theme, which is the default one in Mango RC, then your tiles will be red.
Here is a demo video with the emulator theme changed to green:
That was all about data binding HubTile from the Windows Phone Toolkit - August 2011 (7.1 SDK) in depth. In the next post I will talk about freezing and unfreezing HubTiles, so stay tuned.
The source code is available here:
I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Cool thats what i can say!!!
posted by: Arun vamadevan on 9/30/2011 7:14:02 PM
This control is absolutly brillint and this artile also looks good. thanks for sharing...
Click on HubTile
posted by: mokmap on 12/20/2011 2:20:47 PM
Hello,
I want to know how to open a new page when you click on a hubTile? For example, when I click on the tile Chicken opens a new window it there or get the recipe using chicken?
thank you
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation

