【问题标题】:windows 10 pivot item textWindows 10 枢轴项目文本
【发布时间】:2016-01-15 21:06:26
【问题描述】:

我想在选择标题时更改透视项目标题文本的默认文本样式(前景色、字体粗细等)。

例如,如果我有以下情况:

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

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

谢谢,

【问题讨论】:

    标签: c# xaml uwp windows-10-universal


    【解决方案1】:

    XAML 框架提供了多种自定义应用外观的方法。样式允许您设置控件属性并重复使用这些设置,以在多个控件中保持一致的外观。当您想要自定义控件的视觉结构和视觉行为时,您可以创建控件模板。

    你不需要把pivot header的文字放在边框里,编辑StylePivotHeaderItem会是一个不错的选择,你可以在页面的Resources添加这个样式。

    资源通常是您希望多次使用的某些对象的定义。

    有一个默认的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>
    

    现在,如果您想在选择项目时更改标题中文本的前景,您可以像这样编辑&lt;VisualState x:Name="Selected"&gt;

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

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

    【讨论】:

    • 这让我想起了我不久前遇到的一些事情。各种样式在我的系统上的 C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10240.0\Generic 中一个名为 generic.xml 的文件中定义。我搜索了该文件,找到了 PivotHeaderItem 的样式定义,并通过此处的示例复制了它。我添加了字体粗细修饰符,瞧!就像一个魅力,它奏效了。谢谢,
    【解决方案2】:

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2022-10-12
      • 1970-01-01
      • 2023-04-04
      • 2013-11-08
      • 1970-01-01
      • 2016-01-26
      • 2014-06-14
      • 1970-01-01
      • 2016-06-14
      相关资源
      最近更新 更多