Change Pivot HeaderTemplate after initialization

I am trying to change the HeaderTemplate Pivot when the orientation of the PhoneApplicationPage changes to accommodate a different display property. The end effect I am trying to achieve is to shrink the Pivot Header FontSize in the terrain to make room for more vertical content.

Templates are declared as Resources in XAML;

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="landscapePivotTitleTemplate">
        <TextBlock Text="" Height="0"/>
    </DataTemplate>
    <DataTemplate x:Key="portraitPivotTitleTemplate">
        <TextBlock Text="{Binding}" />
    </DataTemplate>
    <DataTemplate x:Key="portraitPivotHeaderTemplate">
        <TextBlock Text="{Binding}" />
    </DataTemplate>
    <DataTemplate x:Key="landscapePivotHeaderTemplate">
        <TextBlock  Text="{Binding}" FontSize="{StaticResource PhoneFontSizeSmall}" />
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

      

I am assigning DataTemplate from Page FrameworkElement.Resource ResourceDictionary in handler for OrientationChanged;

void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
    if (e.Orientation == PageOrientation.LandscapeLeft || e.Orientation == PageOrientation.LandscapeRight)
    {
        pivot.HeaderTemplate = (DataTemplate)Resources["landscapePivotHeaderTemplate"];
        pivot.TitleTemplate = (DataTemplate)Resources["landscapePivotTitleTemplate"];
    }
    else 
    {
        pivot.HeaderTemplate = (DataTemplate)Resources["portraitPivotHeaderTemplate"];
        pivot.TitleTemplate = (DataTemplate)Resources["portraitPivotTitleTemplate"];
    }            
}

      

Using the templates above, the title is properly hidden by assigning a TitleTemplate. However, it looks like you can only set the header template once, and subsequent assignments to the HeaderTemplate will only be effective by adding new PivotItems. Is there a better way to approach this?

+3


source to share





All Articles