Correct Pivot Resources

Here are the resources for quick copy and pasting. View this blog post about correcting Pivot styles and behaviors.

Styles+Resources

The styles and templates needed to correct issues with the Pivot control so it can behave predictably and match native Pivots in the OS. Include these resources in your App.xaml:

<Thickness x:Key="PivotPortraitThemePadding">19,12,0,0</Thickness>
<Thickness x:Key="PivotLandscapeThemePadding">19,18,0,0</Thickness>
<x:Int32 x:Key="PivotHeaderItemCharacterSpacing">0</x:Int32>
<Thickness x:Key="PivotItemMargin">0,16,0,0</Thickness>
<Thickness x:Key="PivotHeaderItemMargin">19,-6.5,5,0</Thickness>
<Style TargetType="Pivot">
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="{Binding}"
                           OpticalMarginAlignment="TrimSideBearings" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Pivot">
                <Grid HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                      VerticalAlignment="{TemplateBinding VerticalAlignment}"
                      Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="Orientation">
                            <VisualState x:Name="Portrait">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TitleContentControl"
                                                                   Storyboard.TargetProperty="Margin">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource PivotPortraitThemePadding}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Landscape">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="TitleContentControl"
                                                                   Storyboard.TargetProperty="Margin">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource PivotLandscapeThemePadding}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <ContentControl x:Name="TitleContentControl"
                                    Style="{StaticResource PivotTitleContentControlStyle}"
                                    Content="{TemplateBinding Title}"
                                    ContentTemplate="{TemplateBinding TitleTemplate}" />
                    <ScrollViewer x:Name="ScrollViewer"
                                  Margin="{TemplateBinding Padding}"
                                  Grid.Row="1"
                                  HorizontalSnapPointsType="MandatorySingle"
                                  HorizontalSnapPointsAlignment="Center"
                                  HorizontalScrollBarVisibility="Hidden"
                                  VerticalScrollMode="Disabled"
                                  VerticalScrollBarVisibility="Disabled"
                                  VerticalSnapPointsType="None"
                                  VerticalContentAlignment="Stretch"
                                  ZoomMode="Disabled"
                                  Template="{StaticResource ScrollViewerScrollBarlessTemplate}">
                        <PivotPanel x:Name="Panel"
                                               VerticalAlignment="Stretch">
                            <PivotHeaderPanel x:Name="Header">
                                <PivotHeaderPanel.RenderTransform>
                                    <CompositeTransform x:Name="HeaderTranslateTransform"
                                                        TranslateX="0" />
                                </PivotHeaderPanel.RenderTransform>
                            </PivotHeaderPanel>
                            <ItemsPresenter x:Name="PivotItemPresenter">
                                <ItemsPresenter.RenderTransform>
                                    <TranslateTransform x:Name="ItemsPresenterTranslateTransform"
                                                        X="0" />
                                </ItemsPresenter.RenderTransform>
                            </ItemsPresenter>
                        </PivotPanel>
                    </ScrollViewer>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Advertisements

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