【问题标题】:Is there a way to dynamically get the RowHeight of a ListView item for use in setting the width of a Grid column?有没有办法动态获取 ListView 项的 RowHeight 以用于设置 Grid 列的宽度?
【发布时间】:2020-04-05 18:00:34
【问题描述】:

我有一个将 HasUnevenRows 设置为“true”的 ListView。在 ListView.ItemTemplate 我有一个网格,我想动态地将 ColumnDefinition Width 设置为 ListView 项的高度。最终目标是获得一个完美正方形的网格单元格,而不管 Platform 和 ListView 行内容如何。

有没有办法让我获取 ListView 项所在行的当前值并将其设置在 ListView.ItemTemplate 的 ColumnDefinition 中?这是我可以在不使用自定义渲染器的情况下完成的事情吗?

这是一个模拟 XAML,代表我想要实现的目标:

<ListView HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>

                <Grid>
                    <Grid.ColumnDefinitions Width=" LISTVIEW ITEM ROW HEIGHT "/>
                    <Grid.ColumnDefinitions Width="*"/>
                </Grid>

            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【问题讨论】:

    标签: xaml listview xamarin xamarin.forms


    【解决方案1】:

    使用RowHeight属性设置ListView的一行的高度。然后使用 Converter 动态设置网格的宽度。

    创建转换器:

     public class MyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value ;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Xaml:

      <ContentPage.Resources>
        <ResourceDictionary>
            <local:MyConverter x:Key="MyConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <ListView
            x:Name="listView"
            BackgroundColor="Green"
            HasUnevenRows="True"
            ItemsSource="{Binding peoples}"
            RowHeight="100">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid BackgroundColor="Accent">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="{Binding Source={x:Reference listView}, Path=RowHeight, Converter={StaticResource MyConverter}}" />
                                <RowDefinition Height="{Binding Source={x:Reference listView}, Path=RowHeight, Converter={StaticResource MyConverter}}" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="{Binding Source={x:Reference listView}, Path=RowHeight, Converter={StaticResource MyConverter}}" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
    
                            <BoxView
                                Grid.Row="0"
                                Grid.Column="0"
                                BackgroundColor="Black" />
                            <BoxView
                                Grid.Row="0"
                                Grid.Column="1"
                                BackgroundColor="Blue" />
                            <BoxView
                                Grid.Row="1"
                                Grid.Column="0"
                                BackgroundColor="Yellow" />
                            <BoxView
                                Grid.Row="1"
                                Grid.Column="1"
                                BackgroundColor="DarkGray" />
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>    
    

    我使用BoxView 来表示正方形,不管黑色是哪种颜色。

    【讨论】:

    • 谢谢,如果没有办法确定每一行的高度,我想我会这样做。这样做的问题是,当明确设置 RowHeight 时,内容周围的形状因平台而异,所以如果我这样做,我将不得不制作一个 ResourceDictionary 或某种参考来设置每个平台的高度。不是一个糟糕的解决方案,我只是希望有更多程序化的东西。
    • listview会填满空白区域,如果我们不设置该值,当尝试在没有初始值的情况下获取高度时,会抛出错误。您可以尝试获取屏幕高度,该值会因平台而异。
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 2018-01-05
    相关资源
    最近更新 更多