【问题标题】:How to make GridView wrap its contents in XAML?如何使 GridView 将其内容包装在 XAML 中?
【发布时间】:2016-06-02 05:05:28
【问题描述】:

作为帖子的标题,我想制作一个 GridView 可以将其所有内容包装在里面,这意味着如果我有 3 个项目,每个项目都有 50px 的高度,那么如何使 GridView 包含这些项目高度为 50*3=150px。 如何做到这一点,请帮助我!

P/s:我想通过配置 XAML 代码来实现,而不是在 C# 代码中,谢谢!

【问题讨论】:

    标签: wpf xaml gridview


    【解决方案1】:

    我找到了答案!只需将 GridView 的高度设置为 Auto,方法是使其位于 Grid 行内。

                    <Grid Grid.Row="0"
                          Margin="12,24">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="64" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid Opacity=".8"
                              Background="White"
                              Height="64">
                            <Grid Margin="8">
                                <TextBlock Text="Students"
                                           VerticalAlignment="Center"
                                           FontSize="24" />
                            </Grid>
                            <Grid Height="2"
                                  VerticalAlignment="Bottom"
                                  Background="#B0BEC5" />
                        </Grid>
    
                        <GridView Background="White"
                                  Name="grvMatchGA"
                                  Grid.Row="1"
                                  ItemTemplate="{ThemeResource groupItemTemplate}"
                                  Opacity=".8"
                                  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                                  ScrollViewer.VerticalScrollBarVisibility="Disabled"
                                  VerticalAlignment="Stretch">
                        </GridView>
                    </Grid>
    

    【讨论】:

      【解决方案2】:

      如果您不为gridview提供高度但它是孩子,它会自动采用等于孩子身高总和的高度。

      例如,你可以做这样的事情-

      <Grid> <Grid.RowDefinitions> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> </Grid.RowDefinitions> <TextBox Grid.Row="0"/> <TextBox Grid.Row="1"/> <Button Grid.Row="2"/> </Grid>

      【讨论】:

      • 网格没有方向,至少在 Windows Phone 中,伙计:D
      • @iamatsundere181 我的错..这是同时处理多种语言的问题:p 无论如何,我已经编辑了与 xaml 网格视图相关的答案.. :)
      猜你喜欢
      • 2021-08-29
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多