如何设置GridView中的对象填满整个高度?

How to set the objects in the GridView fill the whole height?

如何设置GridView中的item填满整个高度? 我希望 GridView 始终有 4 行。目前我设置图片高度为120.

我的代码:

            <GridView
                Grid.Row="1" 
                x:Name="gridView" 
                SelectionChanged="gridView_SelectionChanged"
                ItemsSource="{Binding}"
                SelectionMode="None"
                IsItemClickEnabled="True"
                ItemClick="gridView_ItemClick">
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Border Background="Black">
                            <Image x:Name="ima" Height="120" Stretch="UniformToFill">
                                <Image.Source>
                                    <BitmapImage x:Name="bm" UriSource="{Binding Pa}"  ImageOpened="bm_ImageOpened"/>
                                </Image.Source>
                            </Image>
                        </Border>
                    </DataTemplate>
                </GridView.ItemTemplate>
                <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid Orientation="Vertical" VerticalChildrenAlignment="Stretch"/>
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>
            </GridView>

创建转换器 class,将 Window 高度除以输入参数:

class DivisionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string culture)
    {
        double windowHeight = Window.Current.Bounds.Height;
        double divideBy = Double.Parse(parameter as string);
        return windowHeight / divideBy;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string culture)
    {
        throw new NotSupportedException();
    }
}

使转换器在 XAML 内可用:

<Page .... >
    <Page.Resources>
        <local:DivisionConverter x:Key="DivisionConverter"/>
    </Page.Resources>

然后使用带有数据绑定的转换器:

<Image
    x:Name="ima"
    Height="{Binding Converter={StaticResource DivisionConverter}, ConverterParameter=5}"
    Stretch="UniformToFill">

请注意,我除以 5,因为项目周围总是有边距和填充,这会阻止您适合 5 行:Image.Height * 5 + margins > Grid.Height