Xamarin.Forms ListView размер к содержанию - PullRequest
2 голосов
/ 10 октября 2019

У меня довольно большая форма (адаптированная в основном для планшетов), в которой GridView вложен в два макета стека и просмотр списка

У меня есть несколько случаев, когда у меня есть ListView, который содержит несколько однострочных элементов,и мне нужно, чтобы размер соответствовал содержанию.

Так что это мой исходный код для этого

<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;

Изображение моего проекта

Просто я хочу устранить этот разрыв между списком и макетом стека

Ответы [ 3 ]

1 голос
/ 10 октября 2019

Вместо использования элемента управления ListView. Просто используйте Stacklayout и добавьте к нему BindableLayout. Это в основном превращает Stacklayout в ListView, но без прокрутки.

<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

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

Здесь есть довольно хороший блог по этой функции. https://devblogs.microsoft.com/xamarin/xamarin-forms-3-5-a-little-bindable-love/

0 голосов
/ 11 октября 2019

Вы можете добавить HasUnevenRows="True" вместо rowheight в свой атрибут listview, это будет работать как HeightRequest="-1", ListItem будет иметь именно ту высоту, которая необходима для заполнения содержимого.

0 голосов
/ 11 октября 2019

Просто установите Spacing="0" в стеке, и если вы хотите удалить интервал между дочерними элементами сетки, установите ColumnSpacing="0" и RowSpacing="0"

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...