不幸的是,很多的造型需要碰巧获得成就。首先你需要为Pivot创建一个样式。最好用Expression Blend来做到这一点。
<Style x:Key="PivotStyle1" TargetType="controls:Pivot">
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Pivot">
<Grid HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Background="{TemplateBinding Background}" CacheMode="BitmapCache" Grid.RowSpan="3"/>
<ContentPresenter ContentTemplate="{TemplateBinding TitleTemplate}" Content="{TemplateBinding Title}" Margin="24,17,0,-7"/>
<controlsPrimitives:PivotHeadersControl x:Name="HeadersListElement" Grid.Row="1"/>
<ItemsPresenter x:Name="PivotItemPresenter" Margin="{TemplateBinding Padding}" Grid.Row="2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
注意样式中有一个PivotHeadersControl
。此控件是一个类型为PivotHeaderItem
的TemplatedItemsControl。这个PivotHeaderItem是我们需要的样式。您可以设置ItemContainerStyle
属性,根据其状态来更改标题的外观。
<Style x:Key="PivotHeaderItemStyle1" TargetType="controlsPrimitives:PivotHeaderItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="21,0,1,0"/>
<Setter Property="CacheMode" Value="BitmapCache"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controlsPrimitives:PivotHeaderItem">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.TargetName="contentPresenter"
Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" To="Gray"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="contentPresenter"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBlock x:Name="contentPresenter"
Text="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Foreground="{StaticResource PhoneAccentBrush}"
Margin="{TemplateBinding Padding}" Opacity=".4"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我修改了默认样式,使其具有TextBlock而不是ContentPresenter。这允许您从Unselected状态故事板设置前景。
请确保您将透视的样式设置为PivotStyle1,并且将PivotHeadersControl的ItemContainerStyle设置为PivotHeaderItemStyle1。您还需要对其进行一些修改,以便按照您的需要进行调整。
感谢您的回答。 – bragboy 2012-07-31 21:41:18
关于如何在Windows Phone 8上实现这一点的任何想法?它似乎不想在我的项目中工作。 – AdvancedREI 2013-10-13 22:24:09
你有没有注意到答案的最后一句话? “确保将透视图的样式设置为PivotStyle1,并且将PivotHeadersControl的ItemContainerStyle设置为PivotHeaderItemStyle1。您还需要对其进行一些修改,以便按照您希望的方式调整大小。” – 2013-10-14 22:03:27