Saturday, April 20, 2013

Issues with Panorama TitleTemplate in Windows Phone 8 Apps

Recently I was playing with a prototype Windows Phone 8 app and wanted to set a custom title and subtitle for my Panorama control. The code looked like this:

XAML:
<phone:panorama name="Pano">
  <phone:Panorama.TitleTemplate>
    <DataTemplate>=
      <StackPanel Margin="14,50,0,0">
        <TextBlock Name="Number" Text="{Binding}" Style="{StaticResource PhoneTextNormalStyle}" Margin="0,0,0,0" >
        </TextBlock>
        <TextBlock Name="Title" Text="{Binding}" Style="{StaticResource PhoneTextSubtleStyle}" Margin="0,0,0,0" />
      </StackPanel>
    </DataTemplate>
  </phone:Panorama.TitleTemplate>
</phone:Panorama>
Unfortunately there doesn't seem to be an easy way to set the binding for the Number and Title fields using the template.  I found an easy solution by nixing the TitleTemplate and just setting the title directly as follows:

New XAML:
<phone:Panorama Name="Pano">
  <phone:Panorama.Title>
      <StackPanel Margin="14,50,0,0">
        <TextBlock Name="Number" Text="{Binding}" Style="{StaticResource PhoneTextNormalStyle}" Margin="0,0,0,0" >
        </TextBlock>
        <TextBlock Name="Title" Text="{Binding}" Style="{StaticResource PhoneTextSubtleStyle}" Margin="0,0,0,0" />
      </StackPanel>
  </phone:Panorama.Title>
</phone:Panorama>
Now I can set the binding in code behind as such:

            this.Number.Text = numberText;
            this.Title.Text = titleText;

Where numberText and titleText are public static strings defined in my code behind.  The resulting appearance is exactly the same as if I'd used the TitleTemplate.

I couldn't find any useful articles online so thought I'd share it here in case anyone else is spending a lot of time trying to figure this out!  Hope this helps you out!

5 comments:

Kate said...

Thanks! Hope you found it useful!

Kate

Derik Whittaker said...

Another option would be to use the TitleTemplate as before. But bind your data context to the title

Title="{Binding .}"

Unknown said...

I likewise have the same issues and i have had my telephone since walk not long from now and the sum of a sudden it keeps comin up with the 80048264 failure and wont let me download any applications, i would prefer not to reset my telephone as my move down isn't working and has thought of an alternate lapse code helllllpppppp????:/mobile app development // mobile app developers // iPhone app maker

Uchiha Itachi said...
This comment has been removed by the author.
Uchiha Itachi said...

WinForms Panorama Control