【问题标题】:How to make Windows 10 pivot/tab headers full width of screen如何使 Windows 10 枢轴/选项卡标题全屏显示
【发布时间】:2016-10-26 09:54:04
【问题描述】:

如何使枢轴/选项卡标题具有相同的宽度并拉伸枢轴标题的整个宽度而不延伸到屏幕之外(特别是对于移动设备)?我还没有找到如何做到这一点的示例。

这是我想要达到的目标:

【问题讨论】:

  • 你查了吗UWP Sample
  • 是的,但它没有全宽屏幕枢轴选项卡的示例。

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


【解决方案1】:

如何使枢轴/选项卡标题使每个单独的选项卡具有相同的宽度并拉伸整个宽度

这可能无法通过仅设置枢轴的style 来实现。但这可以通过ViewTreeHelper 类轻松实现。我们可以尝试通过pivot header的actualwidth和header item总数来计算每个header item的宽度。并手动设置宽度,然后标题项将拉伸整个宽度。

代码示例如下:

 <Pivot  x:Name="CustomPivot">     
       <Pivot.Resources>
            <Style TargetType="PivotHeaderItem">
                <Setter Property="FontSize" Value="{ThemeResource PivotHeaderItemFontSize}" />
                <Setter Property="FontFamily" Value="{ThemeResource PivotHeaderItemFontFamily}" />
                <Setter Property="FontWeight" Value="{ThemeResource PivotHeaderItemThemeFontWeight}" />
                <Setter Property="CharacterSpacing" Value="{ThemeResource PivotHeaderItemCharacterSpacing}" />
                <Setter Property="Background" Value="Gray" />
                <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseMediumBrush}" />
                <Setter Property="Padding" Value="{ThemeResource PivotHeaderItemMargin}" />
                <Setter Property="Height" Value="Auto" />
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="RequestedTheme" Value="Dark" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="PivotHeaderItem">
                            <Grid x:Name="Grid" Background="{TemplateBinding Background}">
                                <Grid.Resources>
                                    <Style x:Key="BaseContentPresenterStyle" TargetType="ContentPresenter">
                                        <Setter Property="FontFamily" Value="Segoe UI" />
                                        <Setter Property="FontWeight" Value="SemiBold" />
                                        <Setter Property="FontSize" Value="15" />
                                        <Setter Property="TextWrapping" Value="Wrap" />
                                        <Setter Property="LineStackingStrategy" Value="MaxHeight" />
                                        <Setter Property="TextLineBounds" Value="Full" />
                                        <Setter Property="OpticalMarginAlignment" Value="TrimSideBearings" />
                                    </Style>
                                    <Style
                                        x:Key="BodyContentPresenterStyle"
                                        BasedOn="{StaticResource BaseContentPresenterStyle}"
                                        TargetType="ContentPresenter">
                                        <Setter Property="FontFamily" Value="{ThemeResource PivotHeaderItemFontFamily}" />
                                        <Setter Property="FontWeight" Value="{ThemeResource PivotHeaderItemThemeFontWeight}" />
                                        <Setter Property="FontSize" Value="{ThemeResource PivotHeaderItemFontSize}" />
                                    </Style>
                                </Grid.Resources>
                                <ContentPresenter
                                    x:Name="ContentPresenter"
                                    Margin="{TemplateBinding Padding}"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    FontFamily="{TemplateBinding FontFamily}"
                                    FontSize="{TemplateBinding FontSize}"
                                    FontWeight="{TemplateBinding FontWeight}"
                                    Content="{TemplateBinding Content}"
                                    ContentTemplate="{TemplateBinding ContentTemplate}">
                                    <ContentPresenter.RenderTransform>
                                        <TranslateTransform x:Name="ContentPresenterTranslateTransform" />
                                    </ContentPresenter.RenderTransform>
                                </ContentPresenter>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="SelectionStates">
                                        <VisualStateGroup.Transitions>
                                            <VisualTransition
                                                From="Unselected"
                                                GeneratedDuration="0:0:0.33"
                                                To="UnselectedLocked" />
                                            <VisualTransition
                                                From="UnselectedLocked"
                                                GeneratedDuration="0:0:0.33"
                                                To="Unselected" />
                                        </VisualStateGroup.Transitions>
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Unselected" />
                                        <VisualState x:Name="UnselectedLocked">
                                            <Storyboard>
                                                <DoubleAnimation
                                                    Duration="0"
                                                    Storyboard.TargetName="ContentPresenterTranslateTransform"
                                                    Storyboard.TargetProperty="X"
                                                    To="{ThemeResource PivotHeaderItemLockedTranslation}" />
                                                <DoubleAnimation
                                                    Duration="0"
                                                    Storyboard.TargetName="ContentPresenter"
                                                    Storyboard.TargetProperty="(UIElement.Opacity)"
                                                    To="0" />
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Selected">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="#FF42424C" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="UnselectedPointerOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="SelectedPointerOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="UnselectedPressed">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="SelectedPressed">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Pivot.Resources>   
     <PivotItem>
         <PivotItem.Header>
             <local:TabHeader Glyph="&#xE719;" Label="item 1" />
         </PivotItem.Header>
         <TextBlock Text="Content content content" />
     </PivotItem>
     <PivotItem>
         <PivotItem.Header>
             <local:TabHeader Glyph="&#xE721;" Label="item 2" />
         </PivotItem.Header>
         <TextBlock Text="Content content content" />
     </PivotItem>
     <PivotItem>
         <PivotItem.Header>
             <local:TabHeader Glyph="&#xE723;" Label="item 3" />
         </PivotItem.Header>
         <TextBlock Text="Content content content" />
     </PivotItem>
 </Pivot>

后面的代码:

 private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
 {
     if (depObj != null)
     {
         for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
         {
             DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
             if (child != null && child is T)
             {
                 yield return (T)child;
             }

             foreach (T childOfChild in FindVisualChildren<T>(child))
             {
                 yield return childOfChild;
             }
         }
     }
 }

 private void Page_Loaded(object sender, RoutedEventArgs e)
 {
     IEnumerable<PivotHeaderPanel> headerpanel = FindVisualChildren<PivotHeaderPanel>(CustomPivot);
     double totalwidth = headerpanel.ElementAt<PivotHeaderPanel>(0).ActualWidth;
     IEnumerable<PivotHeaderItem> items = FindVisualChildren<PivotHeaderItem>(CustomPivot);
     int headitemcount = items.Count();
     for (int i = 0; i < headitemcount; i++)
     {
         items.ElementAt<PivotHeaderItem>(i).Width = totalwidth / headitemcount;
     }
 }

自定义用户控件TabHeader的代码请参考official sample

结果:

【讨论】:

  • 我让它工作,但只有当窗口最小化到它占据整个宽度的区域时。您是否使用任何样式或枢轴资源来实现此结果?
  • @JoeyH,我只是添加了一些关于颜色设置的样式,以帮助您清楚地看到边框。样式不应影响宽度。我不确定您在这里的意思是什么,在我看来,这意味着使应用程序主窗口最小化到工具栏。如果您的意思是当您更改窗口大小时,宽度不会占据整个宽度,请输入上述方法Window.SizeChanged event。但是我还是把我的风格上传到了回复中,大家可以参考一下。
  • 我不想再为这个问题打扰你,但我在一个新项目中再次尝试了这个,但标签的宽度都没有正确移动。我为该项目使用了 Microsoft Samples Pivot Sample 中的代码。我不明白我做错了什么。 i.imgur.com/x7A4rXj.png
  • @JoeyH,样本是否有自己的风格打破了这一点。您的 uwp 应用程序目标版本是什么?上传整个项目让我直接帮助调试。
  • 目标版本是10586,这里是项目:1drv.ms/u/s!AjzDfXpW-m-skcJR31KsfYdS0ldz0A
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-12
  • 1970-01-01
相关资源
最近更新 更多