【问题标题】:The DataTemplate in HubSection doesn't get full heightHubSection 中的 DataTemplate 没有得到全高
【发布时间】:2014-06-13 14:43:49
【问题描述】:

我有以下代码:

<HubSection x:Name="modules_section" ...>
    <DataTemplate>
        <StackPanel Orientation="Vertical">
        ..................................

如何让 StackPanel 占据 HubSection 的全部高度?

VerticalAlignment="Stretch" 不起作用。

我也尝试使用绑定通过

<StackPanel Height={Binding ElementName=modules_section, Path=ActualHeight}" ...>

但集线器部分的实际高度似乎为 0(我也尝试在代码隐藏中设置它并调试以查看值)。

【问题讨论】:

  • 我正在设置我的 windows 8.1 环境来重现这个问题,你能告诉我 HubSection 周围的代码吗?

标签: windows-store-apps winrt-xaml windows-8.1


【解决方案1】:

&lt;StackPanel&gt; 与其中的内容一样大,但通常,要使&lt;HubSection&gt; 中的项目与节中的剩余空间一样高,只需在节上设置VerticalContentAlignment ,即:

<HubSection VerticalContentAlignment="Stretch">
    <DataTemplate>
        <ScrollViewer HorizontalScrollMode="..." VerticalScrollMode="...">
            <StackPanel Orientation="Vertical">
                ...

在这种情况下,我将 &lt;StackPanel&gt; 包裹在 &lt;ScrollViewer&gt; 中,以应对有足够数据需要滚动的情况

【讨论】:

  • 这解决了我在使用 Windows 应用商店应用时遇到的同样问题。除非问题的作者发现它不适合他的情况,否则它应该被接受为答案。
  • @RowlandShaw 出于某种原因,这在我的情况下不起作用...你能看看stackoverflow.com/questions/28760148/…,好吗?提前谢谢你
  • @VasileF 您似乎没有在您的HubSections 上设置VerticalContentAlignment
  • @RowlandShaw,确实我提供的代码在 HubSection 上没有 VerticalContentAlignment。我试过了,并将文本块包装在滚动查看器中,但没有成功......如果您有任何进一步的想法,请回答我发布的问题。抱歉打扰了这篇文章。
【解决方案2】:

如果您想使用所有高度并且不想使用 Hub.Header 属性 你有这个解决方案

在页面的资源中为 HubSection 编写自定义样式:

<Style x:Key="Prova" TargetType="HubSection">
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="HubSection">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <Border.Resources>
                            <ControlTemplate x:Key="HeaderButtonTemplate" TargetType="Button">
                                <Grid Background="Transparent">
                                    <VisualStateManager.VisualStateGroups>
                                        <VisualStateGroup x:Name="CommonStates">
                                            <VisualState x:Name="Normal"/>
                                            <VisualState x:Name="PointerOver">
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource HubSectionHeaderPointerOverForegroundThemeBrush}"/>
                                                    </ObjectAnimationUsingKeyFrames>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="IsHeaderInteractiveMarker">
                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource HubSectionHeaderPointerOverForegroundThemeBrush}"/>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </VisualState>
                                            <VisualState x:Name="Pressed">
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource HubSectionHeaderPressedForegroundThemeBrush}"/>
                                                    </ObjectAnimationUsingKeyFrames>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="IsHeaderInteractiveMarker">
                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource HubSectionHeaderPressedForegroundThemeBrush}"/>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </VisualState>
                                            <VisualState x:Name="Disabled">
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledForegroundThemeBrush}"/>
                                                    </ObjectAnimationUsingKeyFrames>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="IsHeaderInteractiveMarker">
                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonDisabledForegroundThemeBrush}"/>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </VisualState>
                                        </VisualStateGroup>
                                        <VisualStateGroup x:Name="FocusStates">
                                            <VisualState x:Name="Focused">
                                                <Storyboard>
                                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualWhite"/>
                                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualBlack"/>
                                                </Storyboard>
                                            </VisualState>
                                            <VisualState x:Name="Unfocused"/>
                                            <VisualState x:Name="PointerFocused"/>
                                        </VisualStateGroup>
                                        <VisualStateGroup x:Name="IsHeaderInteractiveStates">
                                            <VisualState x:Name="HeaderInteractive">
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="IsHeaderInteractiveMarker">
                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </VisualState>
                                            <VisualState x:Name="HeaderNonInteractive"/>
                                        </VisualStateGroup>
                                        <VisualStateGroup x:Name="FlowDirectionStates">
                                            <VisualState x:Name="LeftToRight"/>
                                            <VisualState x:Name="RightToLeft">
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Text" Storyboard.TargetName="IsHeaderInteractiveMarker">
                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="  &#xE26C;"/>
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </VisualState>
                                        </VisualStateGroup>
                                    </VisualStateManager.VisualStateGroups>
                                    <StackPanel Orientation="Horizontal">
                                        <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" OpticalMarginAlignment="TrimSideBearings" TextLineBounds="Tight" VerticalAlignment="Center"/>
                                        <TextBlock x:Name="IsHeaderInteractiveMarker" AutomationProperties.AccessibilityView="Raw" FontFamily="{ThemeResource SymbolThemeFontFamily}" OpticalMarginAlignment="TrimSideBearings" TextLineBounds="Tight" Text=" &#xE26B;" Visibility="Collapsed" VerticalAlignment="Center"/>
                                    </StackPanel>
                                    <Rectangle x:Name="FocusVisualWhite" IsHitTestVisible="False" Margin="-5" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{ThemeResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1"/>
                                    <Rectangle x:Name="FocusVisualBlack" IsHitTestVisible="False" Margin="-5" Opacity="0" StrokeDashOffset="0.5" StrokeEndLineCap="Square" Stroke="{ThemeResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1"/>
                                </Grid>
                            </ControlTemplate>
                        </Border.Resources>
                        <Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <!--<Rectangle x:Name="HubHeaderPlaceholder" Grid.Row="0"/>-->
                            <!--<Button x:Name="HeaderButton" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" FontWeight="{ThemeResource HubSectionHeaderThemeFontWeight}" FontSize="{ThemeResource HubSectionHeaderThemeFontSize}" Margin="{ThemeResource HubSectionHeaderThemeMargin}" Grid.Row="1" Template="{StaticResource HeaderButtonTemplate}"/>-->
                            <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Grid.Row="2"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

在您的 HubSection 中,您将拥有

<HubSection x:Uid="Section1Header" Style="{StaticResource Prova}">

就是这样

【讨论】:

  • 当你仍然想要标题时怎么办?就我而言,我看到 ScrollViewer 比可用空间高
  • 您可以使用 RowSpan 并取消注释标题部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 2013-02-25
相关资源
最近更新 更多