大小 GridViewItems 以填充 GridView? [英] Size GridViewItems to fill GridView?

查看:18
本文介绍了大小 GridViewItems 以填充 GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题的反面:如何动态调整 GridView 项目的大小?

我有一个两行四列的 GridView.所需的行为是让 GridViewItems 填充单元格,我想不出一种方法来做到这一点.GridViewItem 是一个包含一些其他内容的网格,我将其作为 DataTemplate.我可以为项目提供宽度和高度,但这仅适用于一种分辨率,并且我需要项目是动态的和成比例的,因此如果它们显示在不同的设备上,它们将继续是 2 行,每行 4 个项目.

I have a GridView with two rows and four columns. The desired behavior is for the GridViewItems to fill the cell, and I can't figure out a way to do this. The GridViewItem is a Grid with some other content, which I have as a DataTemplate. I can provide a Width and Height for an item, but that only works for one resolution, and I need the items to be dynamic and proportional, so if they are displayed on a different device they will continue to be 2 rows of 4 items.

在另一个页面上,我有一个具有相同布局的静态网格,每个单元格中都包含矩形.这些会自动拉伸,我已经尝试将它们的宽度和高度存储在我在 Rectangle 的 SizeChanged 上获得的 DependencyProperties 中,但我不知道如何绑定到它们.

On a different page, I have a static Grid with the same layout, containing Rectangles in each cell. These stretch automatically, and I've tried storing their width and height in DependencyProperties that I get on the Rectangle's SizeChanged, but I can't figure out how to bind to them.

页面 XAML 是这样的:

The page XAML is this:

                    <Grid x:Name="Pane2LayoutGrid" 
                    Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="90"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>

                    <TextBlock x:Name="TitleText" Text="Favorites" Style="{StaticResource TitleBarHeader}"/>

                    <GridView x:Name="GridView" 
                        Grid.Row="1" 
                        Margin="0,10,0,8" 
                        Style="{StaticResource GridView}" 
                        ItemsSource="{Binding Items, Source={StaticResource vm}}" 
                        />

                </Grid>

和数据模板:

                <DataTemplate>
                <Grid>

                    <Border x:Name="Border" Style="{StaticResource GridBorder}">

                        <Grid x:Name="grid" Style="{StaticResource Grid}" >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1.3*"/>
                                <ColumnDefinition Width="2*"/>
                            </Grid.ColumnDefinitions>
                            <!-- rows for alignment -->
                            <Grid.RowDefinitions>
                                <RowDefinition Height="1*"/>
                                <RowDefinition Height="3*"/>
                                <RowDefinition Height="1*"/>
                            </Grid.RowDefinitions>
                            <TextBlock x:Name="Text1" Grid.Row="1" Text="{Binding Title}" Style="{StaticResource Title}" />
                            <TextBlock x:Name="Text2" Grid.Row="1" Text="{Binding Subtitle}" Style="{StaticResource SubTitle}" />
                        </Grid>

                    </Border>
                </Grid>
            </DataTemplate>

推荐答案

我的队友 Dwayne 有解决方案,但我们花了一段时间才弄明白.

My teammate Dwayne had the solution, but it took us a while to figure it out.

  • 为 GridViewItem 所需的宽度创建 DependencyProperties 和高度.
  • 在其 SizeChanged 事件上测量 GridView 的大小.(我们的问题是试图获得一个不为零的值——其他事件不会在正确的时间呈现面板.)
  • 将 ItemContainer WrapGrid 的 ItemWidth 和 ItemHeight 绑定到这些 DependencyProperties.在 DataTemplate 中设置宽度和高度不起作用.

一堆单独的小块需要缝合在一起的问题之一.

One of those problems with a bunch of separate little pieces needing stitching together.

这篇关于大小 GridViewItems 以填充 GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆