简体   繁体   English

Windows Phone 8.1 Pivot自定义标题样式

[英]Windows Phone 8.1 Pivot custom header style

I'm aiming to mimic similar effect as seen here: http://www.visuallylocated.com/post/2012/05/23/Changing-the-background-color-of-your-pivot-headers.aspx . 我的目标是模仿这里看到的类似效果: http//www.visuallylocated.com/post/2012/05/23/Changing-the-background-color-of-your-pivot-headers.aspx There are resources online describing how to do it, but all of them apply to Windows Phone 8. 8.1 update brought severe API changes, making the code useless. 网上有资源描述如何操作,但所有这些资源都适用于Windows Phone 8. 8.1更新带来了严重的API更改,使代码无用。

So, how can I style pivot header? 那么,我如何设置pivot头? I found namespace Windows.UI.Xaml.Controls.Primitives , which includes class PivotHeaderPanel, which might be helpful in this situation, but I can't find a way to access this class from XAML. 我发现名称空间Windows.UI.Xaml.Controls.Primitives ,其中包括类PivotHeaderPanel,这可能在这种情况下有用,但我找不到从XAML访问此类的方法。 Or maybe there is another way? 或许还有另一种方式?

If you just want to change the background color of all the headers, this is how you can do it in Window Phone 8.1. 如果您只想更改所有标题的背景颜色,可以在Window Phone 8.1中执行此操作。

First, use Expression Blend to generate the default style of the Pivot control. 首先,使用Expression Blend生成Pivot控件的默认样式。

<Thickness x:Key="PivotPortraitThemePadding">19,38,0,0</Thickness>
<Thickness x:Key="PivotLandscapeThemePadding">19,25,0,0</Thickness>
<Style x:Key="CustomPivotStyle" TargetType="Pivot">
    <Setter Property="Margin" Value="0"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Foreground" Value="{ThemeResource PhoneForegroundBrush}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <Grid/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Pivot">
                <Grid x:Name="RootElement" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="Orientation">
                            <VisualState x:Name="Portrait">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Margin" Storyboard.TargetName="TitleContentControl">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotPortraitThemePadding}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Landscape">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Margin" Storyboard.TargetName="TitleContentControl">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotLandscapeThemePadding}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentControl x:Name="TitleContentControl" ContentTemplate="{TemplateBinding TitleTemplate}" Content="{TemplateBinding Title}" Style="{StaticResource PivotTitleContentControlStyle}"/>
                    <ScrollViewer x:Name="ScrollViewer" HorizontalSnapPointsAlignment="Center" HorizontalSnapPointsType="MandatorySingle" HorizontalScrollBarVisibility="Hidden" Margin="{TemplateBinding Padding}" Grid.Row="1" Template="{StaticResource ScrollViewerScrollBarlessTemplate}" VerticalSnapPointsType="None" VerticalScrollBarVisibility="Disabled" VerticalScrollMode="Disabled" VerticalContentAlignment="Stretch" ZoomMode="Disabled">
                        <PivotPanel x:Name="Panel" VerticalAlignment="Stretch">
                            <PivotHeaderPanel x:Name="Header" Background="{TemplateBinding BorderBrush}">
                                <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>

Find this line below, the only change I have made to the default style is adding Background="{TemplateBinding BorderBrush}" to the PivotHeaderPanel which is the panel that hosts all the headers. 在下面找到这一行,我对默认样式所做的唯一更改是将Background="{TemplateBinding BorderBrush}"PivotHeaderPanel ,后者是托管所有标题的面板。

<PivotHeaderPanel x:Name="Header" Background="{TemplateBinding BorderBrush}">

The reason that I use TemplateBinding here is because doing this gives me the flexibility to change the headers background by specifying the BorderBush of the Pivot . 我在这里使用TemplateBinding的原因是因为这样做可以让我灵活地通过指定PivotBorderBush来更改标题背景。 As the BorderBrush is not used anywhere in the control, there won't be any side effect if we change it. 由于BorderBrush未在控件中的任何位置使用,因此如果我们更改它将不会有任何副作用。

So, all you need to do in your Pivot is this. 所以,你需要在你的Pivot做的就是这个。

<Pivot x:Uid="Pivot" Title="MY APPLICATION" x:Name="pivot" CommonNavigationTransitionInfo.IsStaggerElement="True" Style="{StaticResource CustomPivotStyle}" BorderBrush="{StaticResource PhoneAccentBrush}">

This is how they look now. 这就是他们现在的样子。

在此输入图像描述

Hope this helps! 希望这可以帮助!

So 8.1 killed how I used to do HeaderTemplates. 所以8.1杀了我以前做HeaderTemplates的方式。

My solution now is to put a customized TextBlock or Control in the Header element of the PivotItem. 我现在的解决方案是在PivotItem的Header元素中放置一个自定义的TextBlock或Control。


<Pivot>

    <PivotItem>
        <PivotItem.Header>
            <Grid Background="Red">
                <TextBlock Text="WHY DID YOU DO THIS MICROSOFT"/>
            </Grid>
        </PivotItem.Header>
    </PivotItem>
    ...
</Pivot>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM