PivotItem control windows phone 7 set color of disabled header

 ̄綄美尐妖づ 提交于 2019-12-24 11:35:08

问题


Is there a way to set the state of the header color in XAML when the PivotItem control is not the current selected one.

This is the header code i am using for the pivot item control

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

In the below example, I want all the header's color to be PhoneAccentBrush but when it goes in the disabled state, I want to it to be grey (but it becomes a dimmed version of PhoneAccentBrush). How to do that ? I know for sure I can do this hack in C# code, but I want this done in the XAML itself. Please help.


回答1:


Unfortunately there is a lot of styling that needs to happen to get this accomplished. First you need to create a style for the Pivot. It is best to do this with 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>

Notice in the style that there is a PivotHeadersControl in it. This control is a TemplatedItemsControl of type PivotHeaderItem. This PivotHeaderItem is what we need to style. You can set the ItemContainerStyle property to alter how the headers will look depending on their state.

<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>

I've modified the default style to have a TextBlock instead of a ContentPresenter. This allows you to set the foreground from the Unselected state storyboard.

Make sure that you set the Style of the Pivot to be PivotStyle1 and that the ItemContainerStyle of the PivotHeadersControl is set to PivotHeaderItemStyle1. You'll also need to modify it a little to get the sizing back the way you want it.



来源:https://stackoverflow.com/questions/11746411/pivotitem-control-windows-phone-7-set-color-of-disabled-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!