不幸的是,很多的造型需要碰巧獲得成就。首先你需要爲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