Xamarin Listview动态列宽

时间:2018-01-09 11:43:33

标签: listview xamarin grid

我想知道是否有适当的方法来解决这种情况。想象一下,你将动态数据变成这样的东西:

 <ListView HasUnevenRows="True" 
              ItemsSource="{Binding RecentSurveysList} "
              SelectedItem="{Binding Selected, Mode=TwoWay}"
              ItemSelected="ListView_OnItemSelected">

        <ListView.ItemTemplate>

            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal" Padding="20">
                        <Label Text="{Binding Id}"  VerticalOptions="Start" Margin="0, 0 ,60 ,0"></Label>
                        <Label Text="{Binding Date}" HorizontalOptions="Start" VerticalOptions="Center" Margin="0, 0 ,20 ,0"></Label>
                        <Label Text="{Binding RecentLocationName}" HorizontalOptions="Start" VerticalOptions="Center" Margin="0, 0 ,20 ,0"></Label>
                        <Label Text="{Binding ClientFirstName}" HorizontalOptions="Start" VerticalOptions="Center" Margin="0, 0 ,20 ,0"></Label>
                        <Label Text="{Binding ClientLastName}" HorizontalOptions="Start" VerticalOptions="Center" Margin="0, 0 ,20 ,0"></Label>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>

        </ListView.ItemTemplate>

    </ListView>

问题N1

现在ID可以是123或34816349714,问题是下一个单元格的数据被移位,所以一切都不在网格中。

问题N2

我想为每个列设置一个列标题。

也许listview不是最佳选择,但任何人都有他们在这种情况下使用的现成方法吗?感谢。

1 个答案:

答案 0 :(得分:0)

我使用grid,stacklayout和listview共同管理这个问题。

<StackLayout>

        <ListView HasUnevenRows="True" 
              ItemsSource="{Binding RecentSurveysList} "
              SelectedItem="{Binding Selected, Mode=TwoWay}"
              ItemSelected="ListView_OnItemSelected">

         <ListView.Header>
             <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="2*"/>
                        <ColumnDefinition Width="2*"/>
                        <ColumnDefinition Width="2*"/>
                        <ColumnDefinition Width="3*"/>
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                 <Label Text="Id" BackgroundColor="CornflowerBlue" Grid.Column="0" Grid.Row="0" TextColor="White"/>
                 <Label Text="Date" BackgroundColor="CornflowerBlue" Grid.Column="1" Grid.Row="0" TextColor="White"/>
                 <Label Text="Location" BackgroundColor="CornflowerBlue" Grid.Column="2" Grid.Row="0" TextColor="White"/>
                 <Label Text="Client Firtname" BackgroundColor="CornflowerBlue" Grid.Column="3" Grid.Row="0" TextColor="White"/>
                 <Label Text="Client Lastname" BackgroundColor="CornflowerBlue" Grid.Column="4" Grid.Row="0" TextColor="White"/>

                </Grid>
            </ListView.Header>


            <ListView.ItemTemplate>

            <DataTemplate>

                <ViewCell>

                        <Grid BackgroundColor="White" Margin="0,0,0,1" >

                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*" />
                                <ColumnDefinition Width="2*" />
                                <ColumnDefinition Width="2*" />
                                <ColumnDefinition Width="2*" />
                                <ColumnDefinition Width="3*" />
                            </Grid.ColumnDefinitions>


                            <Label Grid.Column="0" Text="{Binding Id}"  HorizontalOptions="Start" VerticalTextAlignment="Center"/>
                            <Label Grid.Column="1" Text="{Binding Date}" HorizontalOptions="Start" VerticalOptions="Center"></Label>
                            <Label Grid.Column="2" Text="{Binding RecentLocationName}" HorizontalOptions="Start" VerticalOptions="Center"></Label>
                            <Label Grid.Column="3" Text="{Binding ClientFirstName}" HorizontalOptions="Start" VerticalOptions="Center"></Label>
                            <Label Grid.Column="4" Text="{Binding ClientLastName}" HorizontalOptions="Start" VerticalOptions="Center"></Label>


                        </Grid>
                    </ViewCell>

            </DataTemplate>

            </ListView.ItemTemplate>

        </ListView>

    </StackLayout>