windows 10 pivot item text

主宰稳场 提交于 2019-12-11 22:08:32

问题


I want to change the default text style (foreground color, font weight, etc) for pivot item header text when the header is selected.

E.g., if I have the following:

<Pivot>
    <PivotItem Header="One"></PivotItem>
    <PivotItem Header="Two"></PivotItem>
</Pivot>

I want the selected pivot item to be bolded when selected and/or change the foreground color (or maybe put the text in a border, etc). I don't want to change the default style for the unselected items.

Thanx,


回答1:


The XAML framework offers many ways to customize the appearance of your apps. Styles let you set control properties and reuse those settings for a consistent appearance across multiple controls. You create a control template when you want to customize a control's visual structure and visual behavior.

You don't need to put the text of pivot header in a border, edit the Style for PivotHeaderItem would be a good choice, and you can add this style to the Resources of the Page.

Resources are typically definitions of some object that you expect to use more than once.

There is a default PivotHeaderItem styles and templates, you can copy it and add this to you page resources just like this:

<Page
    x:Class="..."
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>
        <Style TargetType="PivotHeaderItem">
             ...
        </Style>
    </Page.Resources>
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        ...
    </Grid>
</Page>

Now if you want to change the foreground of the text in header when the item is selected, you can edit the <VisualState x:Name="Selected"> like this:

<VisualState x:Name="Selected">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

If you want to change the text of header to be bold, you can edit above VisualState like this:

<VisualState x:Name="Selected">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="FontWeight">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Bold" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

You can leave other VisualStates as defalut, if you just want to change the style when the item is selected.




回答2:


In XAML

 <Pivot SelectionChanged="Pivot_SelectionChanged">
        <PivotItem>
            <PivotItem.Header>
                <TextBlock Text="One"></TextBlock>
            </PivotItem.Header>
        </PivotItem>

        <PivotItem>
            <PivotItem.Header>
                <TextBlock Text="Two"></TextBlock>
            </PivotItem.Header>
        </PivotItem>
 </Pivot>

In C#

 private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Pivot pivot = sender as Pivot;
        PivotItem pivotItemSelected = ((PivotItem) ((Pivot) sender).SelectedItem);


        for (int i = 0; i < pivot.Items.Count; i++)
        {
            PivotItem pivotItem = pivot.Items[i] as PivotItem;
            TextBlock tb = pivotItem.Header as TextBlock;
            if (pivotItem == pivotItemSelected)
            {
                //Style 
                tb.Foreground = new SolidColorBrush(Colors.Blue);
            }
            else
            {
                tb.Foreground = new SolidColorBrush(Colors.Black);
            }
        }
    }

I hope this can help you



来源:https://stackoverflow.com/questions/34774515/windows-10-pivot-item-text

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