windows 10 个数据透视项文本

windows 10 pivot item text

我想在选择 header 时更改枢轴项 header 文本的默认文本样式(前景色、字体粗细等)。

例如,如果我有以下内容:

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

我希望选中的枢轴项在选中时加粗 and/or 更改前景色(或者可能将文本放在边框中等)。我不想更改未选中项的默认样式。

谢谢,

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

在 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);
            }
        }
    }

希望对您有所帮助

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.

您不需要将枢轴 header 的文本放在边框中,编辑页面的 Style for PivotHeaderItem would be a good choice, and you can add this style to the Resources

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

有一个默认值PivotHeaderItem styles and templates,你可以复制它并将其添加到你的页面资源中,就像这样:

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

现在,如果您想在选择项目时更改 header 中文本的前景,您可以像这样编辑 <VisualState x:Name="Selected">

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

如果您想将 header 的文本更改为粗体,您可以像这样在 VisualState 上方编辑:

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

如果您只想在选择项目时更改样式,则可以将其他 VisualState 保留为默认值。