Category Archives: WP7

WP7 Navigation Gotcha

This afternoon I discovered a bug in one of the apps I am working on. It happens because the keys we use for navigation are provided in a third-party feed, and one of those keys contains a plus sign (“+”).

This wasn’t immediately apparent as an issue, and we just navigated using

NavigationService.Navigate(new Uri("/Views/TargetView.xaml?key=" + myKeyValue, UriKind.Relative));

Unfortunately, this gets UrlDecoded on the receiving side as a sapce, so our key that started out being “text+text” was being resolved as “text text”. When looking that value up in our tables the key was obviosuly not found and a NullReferenceException was thrown.

The fix was pretty simple, though. We just had to UrlEncode the strong before we sent it, as follows.

NavigationService.Navigate(new Uri("/Views/TargetView.xaml?key=" + System.Net.HttpUtility.UrlEncode(myKeyValue), UriKind.Relative));

and all is good with the world again.

Alter a Pivot controls header template the easy way (WP7)

If you want to alter the template for a Windows Phone 7 pivot control’s header, the simplest way is as follows…

        <controls:Pivot Title="Altered Styles">
            <controls:Pivot.HeaderTemplate>  
                <!-- This changes to look of the items headers -->
                <DataTemplate>
                    <TextBlock Text="{Binding}" Foreground="Black"/>
                </DataTemplate>
            </controls:Pivot.HeaderTemplate>
            <controls:Pivot.TitleTemplate>
                <!-- This changes to look of the pivot overall title -->
                <DataTemplate>
                    <TextBlock Text="{Binding}" Foreground="Black"/>
                </DataTemplate>
            </controls:Pivot.TitleTemplate>
            <controls:PivotItem Header="daily">
                <Grid/>
            </controls:PivotItem>
            <controls:PivotItem Header="hourly">
                <Grid/>
            </controls:PivotItem>
        </controls:Pivot>

Of course, all this example does is specifically set the colour used for the title and headers to black, but you can do (almost) whatever you like in those templates. Want to add an image as a bullet? Go for it.

<controls:Pivot.HeaderTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <Image Height="48" Width="48" Source="/MyBulletImage.png" />
            <TextBlock Text="{Binding}" Foreground="Black"/>
        </StackPanel>
    </DataTemplate>
</controls:Pivot.HeaderTemplate>

Alternating ListBox item background colours in WP7

I had a requirement today to implement alternating row colours in a Windows Phone 7 ListBox. After a bit of frustration with searching and only finding answers tht said “You can’t do it”, or that you need to add a property on the model to bind the background to, I eventually hit upon a nugget of common sense on this page.

Basically you need to create a converter class that will handle the alternation of backgrounds for you. It is ridiculously simple once you see it in action.

Step 1 : Create the converter.

public class AlternateRowColour : IValueConverter
{
bool isAlternate;
SolidColorBrush even = new SolidColorBrush(Colors.Transparent); // Set these two brushes to your alternating background colours.
SolidColorBrush odd = new SolidColorBrush(Color.FromArgb(255, 241, 241, 241));

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
isAlternate = !isAlternate;
return isAlternate ? even : odd ;
}

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

Step 2 : Add the converter to the page

<UserControl
 	...snip...
 	xmlns:conv="clr-namespace:MyApplication.Converters" 
 	...snip...
 	>
	<UserControl.Resources>
		<conv:AlternateRowColour x:Key="RowColour" />
	</UserControl.Resources>
	...snip...
</UserControl>

Step 3 : Bind to the ListBox

<ListBox ItemsSource="{Binding}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <Grid Background="{Binding Converter={StaticResource RowColour}}">
        <!-- layout XAML -->
      </Grid>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>				

And you’re done.