ListView внутри StackLayout: как автоматически изменить размер ListView?

Я помещаю ListView и метку сразу после нее, но на экране есть пустое пространство между ListView и меткой с текстом мс. ListView Windows 10 Mobile

ListView Android Я хочу, чтобы ListView расширялся автоматически при наличии элементов. Или есть ItemsControl, как WPF?

У меня есть этот XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Forms"             
             x:Class="Forms.MainPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <local:MyConverter x:Key="MyConverter"></local:MyConverter>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ScrollView Orientation="Vertical">
        <StackLayout Padding="10,10,10,10">
            <Label FontSize="20" FontAttributes="Bold">Cartão de Ponto</Label>
            <Label>Nome:</Label>
            <Label Text="{Binding Path=Pessoa.Nome}" />
            <Label>CPF:</Label>
            <Label Text="{Binding Path=Pessoa.CPF}" />

            <Grid Padding="0" MinimumHeightRequest="40">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="70" />
                    <ColumnDefinition Width="35" />
                    <ColumnDefinition Width="60" />
                    <ColumnDefinition Width="60" />
                    <ColumnDefinition Width="60" />
                </Grid.ColumnDefinitions>
                <Label FontSize="10" Text="Data" FontAttributes="Bold" VerticalTextAlignment="End" />
                <Label FontSize="10" Grid.Column="1" Text="Dia" FontAttributes="Bold" VerticalTextAlignment="End" />
                <Label FontSize="10" Grid.Column="2" Text="Hora Entrada" FontAttributes="Bold" VerticalTextAlignment="End" />
                <Label FontSize="10" Grid.Column="3" Text="Hora Saída" FontAttributes="Bold" VerticalTextAlignment="End" />
                <Label FontSize="10" Grid.Column="4" Text="Horas Trabalhadas" HorizontalTextAlignment="End" FontAttributes="Bold" />
            </Grid>

            <!-- HeightRequest="0"-->
            <ListView x:Name="lv1" ItemsSource="{Binding Path=Pontos}" 
                      VerticalOptions="FillAndExpand"
                        HasUnevenRows="False"
                      >
                <ListView.Header>
                    <StackLayout HeightRequest="0" />
                </ListView.Header>
                <!-- ItemTemplate -->
                <ListView.Footer>
                    <StackLayout HeightRequest="0" />
                </ListView.Footer>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.View>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="70" />
                                        <ColumnDefinition Width="35" />
                                        <ColumnDefinition Width="60" />
                                        <ColumnDefinition Width="60" />
                                        <ColumnDefinition Width="60" />
                                    </Grid.ColumnDefinitions>
                                    <Label FontSize="12" Text="{Binding Path=Data, StringFormat='{0:dd/MM/yyyy}'}" />
                                    <Label FontSize="12" Grid.Column="1" Text="{Binding Path=DiaSemana}" />
                                    <Label FontSize="12" Grid.Column="2" Text="{Binding Path=HoraEntrada}" />
                                    <Label FontSize="12" Grid.Column="3" Text="{Binding Path=HoraSaida}" />
                                    <Label FontSize="12" Grid.Column="4" Text="{Binding Path=HorasTrabalhadas, Converter={StaticResource MyConverter}}}" />
                                </Grid>
                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <!-- Why is this White space on screen? -->
            <StackLayout Orientation="Horizontal">
                <Label x:Name="lbl1" />
                <Label>ms</Label>
            </StackLayout>

            <Button x:Name="ButtonPegaCartaoPonto" Text="Carregar" />

            <Label></Label>
            <Label></Label>
        </StackLayout>
    </ScrollView>
</ContentPage>

Но ListView lv1 не расширяется автоматически по горизонтали для размещения элементов.

Как это сделать?


person Tony    schedule 05.10.2017    source источник


Ответы (1)


Установите свойство RowHeight вашего ListView и обновите высоту списка, используя код в вашем конструкторе, например:

lv1.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) =>
            {
                if (e.PropertyName == "ItemsSource")
                {
                    try
                    {
                        if (lv1.ItemsSource != null)
                        {
                            var tmp = (IList) lv1.ItemsSource;
                            lv1.HeightRequest = tmp.Count * lv1.RowHeight;
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }
            };

ИЛИ

Вы также можете указать конкретную высоту в зависимости от высоты строки.

lv1.PropertyChanged += (object sender, System.ComponentModel.PropertyChangedEventArgs e) =>
                {
                    if (e.PropertyName == "ItemsSource")
                    {
                        try
                        {
                            if (lv1.ItemsSource != null)
                            {
                                var tmp = (IList) lv1.ItemsSource;
                                lv1.HeightRequest = tmp.Count * 40;
                            }
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                };
person Ziyad Godil    schedule 06.10.2017
comment
Я попробую, и расскажу здесь. - person Tony; 11.10.2017