2012-07-31 73 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

相关问题