Metro Nuggets

Bitesized tidbits for building Modern (Metro) apps.

PhoneListBox Control for Windows Phone

There are a few things I’ve been doing to work around a couple of scenarios when it comes to using the ListBox in Windows Phone. The first is when your ListBox has no items I want it to show something to the user stating there are no items, the other involved limiting the number of items that are enabled based on whether the app was in its Trial Mode.

The first scenario had seen me using a TextBlock and a ListBox and hiding one or the other depending on how many items were in the ListBox, it wasn’t really ideal. The other scenario, well, I didn’t have any workaround for that, but I already had what I’m about to show you done for the first scenario, so modified it for the second scenario. Read on to find out more.

The Solution

I have created a custom implementation of the ListBox that I have cunningly called PhoneListBox, which can be downloaded from my SkyDrive. Drop the dll into your project (or download from nuget, more on that later) and add it as a reference.

Usage

In your xaml, put the following namespace:

xmlns:scott="clr-namespace:ScottIsAFool.WindowsPhone.Controls"

Then you can add the control as you would with a normal ListBox.

<scott:PhoneListBox ItemsSource="{Binding MyItemList}">

</scott:PhoneListBox>

There are a few properties to the PhoneListBox which I’ll go through now.

Text
This is the text that you want to appear should there be no items in your list.

TextHorizontalAlignment
The HorizontalAlignment of the text. The default is HorizontalAlignment.Center.

TextVerticalAlignment
The VerticalAlignment of the text. The default is VerticalAlignment.Top.

TextStyle
The Style you want to apply to the text (like PhoneTextLargeStyle).

IsTrialMode
This is a boolean that sets whether to enable the TrialMode functionality. When enabled, it will disable all items in your list that are after whatever the TrialModeLimit is set as. The default for this is false.

TrialModeLimit
This is the number of items you want to be enabled during Trial Mode. The default for this is 2.

How to get it

You can get it one of two ways, either download it from my SkyDrive (which includes the source code), or you can download it through nuget.

image

If you have any feedback, please email me at scottisafool [at] live.co.uk or on twitter.

SL

Advertisements

One response to “PhoneListBox Control for Windows Phone

  1. Pingback: PhoneListBox Control for Windows Phone

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: