如何在 XAML 中自动调整列表视图的高度

2024-05-01

我的列表视图对象接收图像、ID 号和概要。概要的大小各不相同,因为有些有空格返回。我注意到 ListView 有一个可以设置的行高(我现在设置为 250),但它只能是一个固定值。那么会发生什么,我的网格对于 ListView 来说变得太大,导致它溢出并覆盖到下一个列出的项目上。有没有办法在 XAML 中自动调整列表视图的大小?

<ListView ItemsSource="{Binding List}" VerticalOptions="FillAndExpand" RowHeight="250" SelectedItem="SelectedCTR" SeparatorVisibility="None">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Image Grid.Row="0" HeightRequest="100" MinimumWidthRequest="160" WidthRequest="160" Source="{Binding AttachedmentData,Converter={StaticResource stringToImage}}" />
                    <StackLayout Grid.Row="1" VerticalOptions="FillAndExpand">
                        <Label Text="{Binding Number}" Font="19"
                             TextColor="#f35e20" />
                        <Label Text="{Binding TrimmedSynopsis}" Font="17"
                             TextColor="#503026" />
                    </StackLayout>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

You need to add HasUnevenRows to True and let unset the RowHeight property.

<ListView ItemsSource="{Binding List}" VerticalOptions="FillAndExpand" HasUnevenRows="True" SelectedItem="SelectedCTR" SeparatorVisibility="None">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Image Grid.Row="0" HeightRequest="100" MinimumWidthRequest="160" WidthRequest="160" Source="{Binding AttachedmentData,Converter={StaticResource stringToImage}}" />
                    <StackLayout Grid.Row="1" VerticalOptions="FillAndExpand">
                        <Label Text="{Binding Number}" Font="19"
                             TextColor="#f35e20" />
                        <Label Text="{Binding TrimmedSynopsis}" Font="17"
                             TextColor="#503026" />
                    </StackLayout>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 XAML 中自动调整列表视图的高度 的相关文章

随机推荐