Xamarin.Forms ListView 大小到内容 [英] Xamarin.Forms ListView size to content

查看:28
本文介绍了Xamarin.Forms ListView 大小到内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的表单(主要适用于平板电脑),它有一个 GridView 嵌套了两个 stacklayouts 和 listview

I have a pretty large form (adapted mainly for tablets), that has a GridView nesting a two stacklayouts and listview

我很少有一个 ListView 包含几个单行项目的情况,我需要将它调整到内容大小.

I have few occurrences where I have a ListView that contains a few single-line items, and I need it to size to content.

这是我的源代码

<Grid Grid.Row="2" Grid.ColumnSpan="2" HorizontalOptions="FillAndExpand" x:Name="OrderPricingDetails">

                        <Grid.RowDefinitions>

                            <RowDefinition Height="90"/>

                            <RowDefinition Height="auto" x:Name="OrderPricingDetailsListRow"/>

                            <RowDefinition Height="90"/>

                        </Grid.RowDefinitions>

                        <StackLayout Grid.Row="0" Orientation="Vertical" Margin="10" Padding="10" x:Name="PackageSelectionSection" >
                            <Label Text="Order Pricing" FontAttributes="Bold" TextColor="Black" />
                            <Label Text="Listing details for this specific order" FontSize="Small" TextColor="#757575" />
                            <Frame CornerRadius="5" BackgroundColor="Transparent" HeightRequest="270" BorderColor="#c3c3c3" Padding="10,10,5,10" Margin="0,20,0,0">
                                <StackLayout>

                                    <FlexLayout JustifyContent="SpaceBetween" HorizontalOptions="FillAndExpand" Direction="Row" AlignItems="Center" x:Name="SelectedPackageSection">
                                        <StackLayout Orientation="Horizontal">
                                            <Label Text="Selected Package     " FontSize="Medium" />
                                        </StackLayout>
                                        <Label Text="ALA_CARTE_300" x:Name="selectedOfrferDesc" TextColor="#757575" FontAttributes="Bold" FontSize="Small" Grid.Column="1" Grid.ColumnSpan="2"/>
                                        <Label Text="for" FontSize="Medium" />
                                        <Label Text="LCD" x:Name="selectedOfferPrice" TextColor="#757575" FontAttributes="Bold" FontSize="Small" Margin="0,0,20,0" />
                                    </FlexLayout>

                                </StackLayout>
                            </Frame>
                        </StackLayout>

                        <StackLayout Grid.Row="1" Orientation="Vertical" Margin="10" Padding="10" x:Name="ConnectionfeeSection" HorizontalOptions="FillAndExpand">

                            <ListView x:Name="OfferList" HasUnevenRows="True" VerticalOptions="Center" Margin="0,10,0,0" SelectionMode="None">
                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>

                                            <Frame CornerRadius="5" OutlineColor="#c3c3c3" Padding="5,10,5,5" Margin="0,10,0,0" BackgroundColor="Transparent">
                                                <StackLayout Orientation="Horizontal" Padding="5" BackgroundColor="Transparent" VerticalOptions="Center" >

                                                    <FlexLayout JustifyContent="SpaceBetween" HorizontalOptions="FillAndExpand" Direction="Row" AlignItems="Center" x:Name="SelectedConnectionfeeSection">
                                                        <StackLayout Orientation="Horizontal">
                                                            <Label Text="{Binding DisplayName}" FontSize="Medium" />
                                                        </StackLayout>
                                                        <Label Text="{Binding Amount}" x:Name="selectedConnectionfee" TextColor="#757575" FontAttributes="Bold" FontSize="Small" Margin="0,0,20,0" />
                                                    </FlexLayout>

                                                </StackLayout>
                                            </Frame>

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

                        </StackLayout>

                        <StackLayout Grid.Row="2" Orientation="Vertical" Margin="10" Padding="10" x:Name="ConnectionTotalSection" >

                            <Frame CornerRadius="5" BackgroundColor="Transparent" BorderColor="#c3c3c3" Padding="20,10,5,10" Margin="0,10,0,0">

                                <StackLayout>
                                    <FlexLayout JustifyContent="SpaceBetween" HorizontalOptions="FillAndExpand" Direction="Row" AlignItems="Center" x:Name="SelectedConnectionTotalSection">
                                        <StackLayout Orientation="Horizontal">
                                            <Label Text="Total     " FontSize="Medium" />
                                        </StackLayout>
                                        <Label Text="{Binding Total, StringFormat='{0:N}'}" x:Name="selectedConnectionTotal" TextColor="#757575" FontAttributes="Bold" FontSize="Small" Margin="0,0,20,0" />
                                    </FlexLayout>

                                </StackLayout>
                            </Frame>

                        </StackLayout>

                    </Grid>

这是我背后的代码.

        OrderPricingDetailsListRow.Height = 280;

        OfferList.ItemsSource = connectionsNames;

        int i = connectionsNames.Count;
        int heightRowList = 90;
        i = (i * heightRowList);
        OfferList.HeightRequest = i;

我的项目图片

我只是想消除列表视图和堆栈布局之间的差距

Simply I want to remove that gap between listview and stack layout

推荐答案

而不是使用 ListView 控件.只需使用 Stacklayout 并向其添加 BindableLayout 即可.它基本上将 Stacklayout 变成了一个 ListView 但没有滚动.

Instead of using a ListView control. Just use a Stacklayout and add a BindableLayout to it. It basically turns the Stacklayout into a ListView but without the scrolling.

<StackLayout x:Name="OfferList" BindableLayout.ItemsSource="{Binding ConnectionNames}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Frame CornerRadius="5" OutlineColor="#c3c3c3" Padding="5,10,5,5" Margin="0,10,0,0" BackgroundColor="Transparent">
                <StackLayout Orientation="Horizontal" Padding="5" BackgroundColor="Transparent" VerticalOptions="Center" >
                    <FlexLayout JustifyContent="SpaceBetween" HorizontalOptions="FillAndExpand" Direction="Row" AlignItems="Center" x:Name="SelectedConnectionfeeSection">
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding DisplayName}" FontSize="Medium" />
                        </StackLayout>
                        <Label Text="{Binding Amount}" x:Name="selectedConnectionfee" TextColor="#757575" FontAttributes="Bold" FontSize="Small" Margin="0,0,20,0" />
                    </FlexLayout>

                </StackLayout>
            </Frame>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

或者你也可以用 C# 代码来做,因为我注意到你没有绑定你的 ListView

Or you can do it in C# code too since I noticed you are not binding your ListView

DataTemplate useItemTemplate = null;
BindableLayout.SetItemTemplate(usersPanel, userItemTemplate);

这里有一个关于此功能的不错的博客.https://devblogs.microsoft.com/xamarin/xamarin-forms-3-5-a-little-bindable-love/

There is a pretty good blog on this feature here. https://devblogs.microsoft.com/xamarin/xamarin-forms-3-5-a-little-bindable-love/

这篇关于Xamarin.Forms ListView 大小到内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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