【问题标题】:XAML: How to make a stackpanel fill the screen? (WinRT)XAML:如何使堆栈面板填满屏幕? (WinRT)
【发布时间】:2013-08-17 19:45:16
【问题描述】:
<StackPanel Orientation="Horizontal" Margin="10,43,0,0">
            <RichTextBlock x:Name="MYRTB" HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="#FFEE0000" UseLayoutRounding="True">
                <Paragraph Foreground="#FFFD0000">
                    <Run Text="Testtest"/>
                </Paragraph>
            </RichTextBlock>
            <ListView x:Name="MyListViewNr1">
                <ListViewItem Content="ListView Entry 1"/>
                <ListViewItem Content="ListView Entry 2"/>
                <ListViewItem Content="ListView Entry 2888"/>
            </ListView>
        </StackPanel>

我正在尝试让这个 StackPanel 填满我的 WinRT 应用程序的屏幕。我已经为宽度和高度使用了“自动”属性,但是当我将 Listview 条目或段落添加到足够长的 RichTextBlock 时,文本就会出现在屏幕之外。

如果我对 StackPanel 使用垂直方向,则宽度会自动调整,但高度不会自动调整,这意味着如果有足够的列表视图条目,它们只会进入屏幕“下方”。

实际上有没有办法通过 XAML 执行此操作,或者我是否需要通过代码执行此操作(此处使用 C#)。如果是这样,我如何访问我的应用程序的当前宽度/高度,因为我想让它针对不同的分辨率和方向(横向/纵向)自动调整大小。

提前谢谢你!

【问题讨论】:

  • 您可能需要考虑将 StackPanel 包装在 ScrollViewer 中,这样您的内容就不会像您描述的那样进入屏幕“下方”。
  • 尝试将 StackPanel 替换为 Grid.RowDefinitions 中包含两行的 Grid。将 RichTextBlock 放在 Grid.Row="0" 中,将 Listview 放在 Grid.Row="1" 中。这样看起来会更好,控件会自动扩展。
  • Kiewic 的解决方案奏效了。一个带有 Width="*" 的简单 Grid 完成了这项工作。谢谢你们(对不起,我的英语不好,我不是母语人士)
  • @Kiewic 将其作为答案发布,以便 GenericUser123 可以接受它

标签: c# xaml windows-store-apps winrt-xaml stackpanel


【解决方案1】:

如果您需要为 Windows 8 提供集线器 (Content control for complex layout) 之类的东西,您应该将 GridView 与不同的项目模板一起使用。例如:http://blogs.msdn.com/b/synergist/archive/2012/09/25/windows-store-app-xaml-gridview-with-variable-templates.aspx

【讨论】:

    【解决方案2】:

    使用 Grid 代替 StackPanel

    <Grid Margin="10,43,0,0">
                <RichTextBlock x:Name="MYRTB" HorizontalAlignment="Center"   VerticalAlignment="Top" Foreground="#FFEE0000" UseLayoutRounding="True">
                    <Paragraph Foreground="#FFFD0000">
                        <Run Text="Testtest"/>
                    </Paragraph>
                </RichTextBlock>
                <ListView x:Name="MyListViewNr1">
                    <ListViewItem Content="ListView Entry 1"/>
                    <ListViewItem Content="ListView Entry 2"/>
                    <ListViewItem Content="ListView Entry 2888"/>
                </ListView>
      </Grid>
    

    试试这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-20
      • 2020-07-27
      • 2012-11-02
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多