【问题标题】:StackPanel don't grow with Textblock in Windows Phone 8 ApplicationStackPanel 不会随着 Windows Phone 8 应用程序中的 Textblock 一起增长
【发布时间】:2013-07-20 12:26:57
【问题描述】:

我正在尝试以 3 个文本块在应用程序页面中显示文本。我想滚动它,因为文本是动态更改的,它取决于我们在上一页中选择的内容。当我这样做时:

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Asystent Barmana" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock Name="PageName" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>
    <ScrollViewer Name="Scroller"  HorizontalAlignment="Center" Height="auto" Margin="12,10,10,-1055" Grid.Row="1" VerticalAlignment="Top" Width="460" ManipulationMode="Control" MaxHeight="2500" >
        <StackPanel HorizontalAlignment="Left" Height="auto" VerticalAlignment="Top" Width="460">
            <TextBlock Name="MissingSkladnik" TextWrapping="Wrap" Height="auto" FontSize="24"/>
            <TextBlock Name="Skladniki" TextWrapping="Wrap" Height="auto" FontSize="24"/>
            <TextBlock Name="Przepis" TextWrapping="Wrap" Height="auto" FontSize="24"/>
        </StackPanel>
    </ScrollViewer>

</Grid>

并且文本不是那么长它可以正常工作。但在某些情况下,文本较长,部分文本被截断。当我将 StackPanel 高度更改为 1950 时,它显示得很好,但是当我有较短的文本时,页面末尾是黑色的,没有可滚动的内容:/ 有什么想法吗?

PS。我为我的英语道歉,我已经有一段时间没有使用它了;)

编辑:

我阅读了 cmets 并将堆栈面板更改为网格。我是这样做的:

<ScrollViewer Name="Scroller"  HorizontalAlignment="Center" Height="auto" Margin="12,10,10,-1055" Grid.Row="1" VerticalAlignment="Top" Width="460" ManipulationMode="Control" MaxHeight="2500" >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBlock Name="MissingSkladnik" TextWrapping="Wrap" Height="auto" FontSize="24" Grid.Row="0"/>
            <TextBlock Name="Skladniki" TextWrapping="Wrap" Height="auto" FontSize="24" Grid.Row="1"/>
            <TextBlock Name="Przepis" TextWrapping="Wrap" Height="auto" FontSize="24" Grid.Row="2"/>
        </Grid>
    </ScrollViewer>

它也不起作用。如果我设置 stackpanel 的高度,它可以工作,但是当有少量文本时,它看起来不太好。用户可以 srcoll 黑空屏幕:/

【问题讨论】:

  • 您可以尝试使用网格而不是堆栈面板吗?
  • 为您的堆栈面板设置一些高度..然后尝试
  • 我会先尝试像@KooKiz 所说的那样使用网格。

标签: wpf windows-phone-8 scrollviewer


【解决方案1】:

StackPanel 不会调整其内容的大小,就像Canvas 不会调整其内容的大小一样。但是,Grid 控件可以调整其内容的大小。尝试替换这个:

<StackPanel Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock Text="Asystent Barmana" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Name="PageName" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

用这个:

<Grid Name="TitleGrid" Grid.Row="0" Margin="12,17,0,28">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Text="Asystent Barmana" 
        Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Grid.Row="1" Name="PageName" Text="page name" Margin="9,-7,0,0" 
        Style="{StaticResource PhoneTextTitle1Style}"/>
</Grid>

更新>>>

在 WPF 中,设置显式高度和/或宽度尺寸和/或边距将阻止 Control 自行调整大小。如果您希望 ScrollViewer 将自身重新调整为其内容的大小,请尝试删除您设置的显式布局和尺寸值,例如。 Margin.

【讨论】:

  • 我在滚动查看器中的堆栈面板有问题,而不是在这个 :)
  • 然后将其中的StackPanel 替换为Grid
  • 我也这样做了。在原始帖子中进行了编辑,我在那里添加了我所做的网格代码。它也不起作用
  • 很高兴我能帮上忙。 :)
猜你喜欢
  • 2013-12-13
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 1970-01-01
  • 2013-12-09
相关资源
最近更新 更多