Home > Software engineering >  Is there a way for a ListView within Grid to inherit the grid's ColumnDefinitions & RowDefiniti
Is there a way for a ListView within Grid to inherit the grid's ColumnDefinitions & RowDefiniti

Time:12-15

I'm essentially trying to create a view that displays a list of items underneath an interactable header (a Button and an Image wrapped in a FlexLayout) that sorts that specific column A-Z / Z-A, my problem is that I can't figure out how to line them up neatly and without manually setting the column width in ColumnDefinition.

Normally I'd just set the width to some value but since its going to be cross platform (including tablets) I want to avoid doing that so I don't end up with everything squished to one side if the user decides to use a tablet with a much wider screen, does anyone know any fixes or have any advice on what I could look into trying?

Thanks in advance, and I'll post a snippet of what I'm trying to achieve below

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <FlexLayout AlignItems="Center" Grid.Row="0" Grid.Column="0">
        <Button Text="Name" FontSize="8" Clicked="QuickSort" Padding="0"/>
        <Image Source="" Aspect="AspectFit" HeightRequest="20" WidthRequest="20" IsVisible="False"/>
    </FlexLayout>

    <FlexLayout AlignItems="Center" Grid.Row="0" Grid.Column="1">
        <Button Text="Type" FontSize="8" Clicked="QuickSort" Padding="0"/>
        <Image Source=""  Aspect="AspectFit" HeightRequest="20" WidthRequest="20" IsVisible="False"/>
    </FlexLayout>

    <FlexLayout AlignItems="Center" Grid.Row="0" Grid.Column="2">
        <Button Text="Cost" FontSize="8" Clicked="QuickSort" Padding="0"/>
        <Image Source=""  Aspect="AspectFit" HeightRequest="20" WidthRequest="20" IsVisible="False"/>
    </FlexLayout>

    <ListView ItemSource="{Binding Collection}" Grid.Row="1">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Label Text="{Binding name}" Grid.Column="0"/>
                    <Label Text="{Binding type}" Grid.Column="1"/>
                    <Label Text="{Binding cost}" Grid.Column="2"/>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

CodePudding user response:

A simple solution is to copy the Grid column declarations into the ItemTemplate's DataTemplate. This makes each item a Grid, whose columns match the outer Grid:

<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <Grid ColumnDefinitions="*, *, *">
                <Label Text="{Binding name}" Grid.Column="0"/>
                <Label Text="{Binding type}" Grid.Column="1"/>
                <Label Text="{Binding cost}" Grid.Column="2"/>
            </Grid>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>

The labels all default to "Row=0", so each item is a one-row Grid.

CodePudding user response:

If you like to make it easier, maybe try some datagrid libraries like https://github.com/akgulebubekir/Xamarin.Forms.DataGrid

  • Related