2012-07-31 74 views
1

當PivotItem控件不是當前選定的控件時,是否有方法可以設置XAML中標題顏色的狀態。PivotItem控件windows phone 7設置禁用標題的顏色

這是我使用的支點項控制

<controls:PivotItem.Header> 
    <TextBlock Text="first" Foreground="{StaticResource PhoneAccentBrush}"/> 
</controls:PivotItem.Header> 

在下面的例子中,頭碼,我想所有的頭的顏色是PhoneAccentBrush,但是當它處於禁用狀態,我想它是灰色的(但它變成了PhoneAccentBrush的變暗版本)。怎麼做 ?我知道我可以在C#代碼中進行這種攻擊,但是我希望在XAML本身中完成此操作。請幫忙。

enter image description here

回答

3

不幸的是,很多的造型需要碰巧獲得成就。首先你需要爲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。您還需要對其進行一些修改,以便按照您的需要進行調整。

+0

感謝您的回答。 – bragboy 2012-07-31 21:41:18

+0

關於如何在Windows Phone 8上實現這一點的任何想法?它似乎不想在我的項目中工作。 – AdvancedREI 2013-10-13 22:24:09

+0

你有沒有注意到答案的最後一句話? 「確保將透視圖的樣式設置爲PivotStyle1,並且將PivotHeadersControl的ItemContainerStyle設置爲PivotHeaderItemStyle1。您還需要對其進行一些修改,以便按照您希望的方式調整大小。」 – 2013-10-14 22:03:27

相關問題