Home > Software engineering >  How to make Grid Row dynamic in CollectionView control of .Net MAUI?
How to make Grid Row dynamic in CollectionView control of .Net MAUI?

Time:12-23

I'm trying to achieve dynamic row height in CollectionView control, so that when ever I have more text for a particular property it will extend the height of the frame automatically.

I have tried with ListView as well using HasUnEvenRow property "true" but with that also it's not working.

Here is my code:

<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
    <Grid RowDefinitions="*,60" BackgroundColor="{StaticResource PageBackgroundColor}" Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <CollectionView Grid.Row="0" HorizontalOptions="Fill" VerticalOptions="Fill"
                        ItemsSource="{Binding Inspections}"
                        VerticalScrollBarVisibility="Always"
                        ItemsLayout="VerticalList" ItemSizingStrategy="MeasureAllItems">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Frame Padding="15" HasShadow="False">
                        <Grid HorizontalOptions="Fill"
                                VerticalOptions="Fill"
                                BackgroundColor="White"
                                RowSpacing="25"
                                RowDefinitions="Auto,Auto,Auto"
                                ColumnDefinitions="*,Auto">
                            <StackLayout
                                Orientation="Horizontal"
                                Grid.Row="0"
                                Grid.Column="0">
                                <Label Text="{Binding Path=BusinessName}"
                                        Style="{StaticResource LabelTitleStyle}" />
                                <Grid Padding="15,0,0,0">
                                    <baseChip:Chip
                                        HorizontalOptions="Fill" VerticalOptions="Fill"
                                        Style="{StaticResource ChipContainer}"
                                        HasShadow="False"
                                        BackgroundColor="{Binding Path=ChipBackgroundColor}">
                                    </baseChip:Chip>
                                    <Label
                                        Text="{Binding Path=ChipText}" 
                                        Style="{StaticResource ChipLabel}"
                                        HorizontalOptions="CenterAndExpand"
                                        VerticalOptions="CenterAndExpand"
                                        TextColor="{Binding Path=ChipTextColor}"/>
                                </Grid>
                            </StackLayout>

                            <Image
                                Grid.Row="0"  
                                Grid.Column="1"
                                HorizontalOptions="End"
                                VerticalOptions="Center"
                                Aspect="AspectFit">
                                <Image.Source>
                                    <FontImageSource Glyph="{x:Static helper:MaterialFontHelper.FilePdfBox}"
                                                        Color="{StaticResource DarkGray}"
                                                        Size="20"
                                                        FontFamily="MaterialDesignIcons"/>
                                </Image.Source>
                            </Image>


                            <Grid Grid.Row="1"
                                    Grid.Column="0"
                                    Grid.ColumnSpan="2"
                                    RowDefinitions="Auto, Auto"
                                    ColumnDefinitions="*, *">

                                <Label
                                    Grid.Row="0"
                                    Grid.Column="0"
                                    Text="Inspection Type"
                                    Style="{StaticResource LabelKeyStyle}" />

                                <Label
                                    Grid.Row="1"
                                    Grid.Column="0"
                                    Text="ksd kahdkahd kahd kahd  aojsoiud aasjlj sdlkja dlkja da asdadas  alsajdlaksjdlajd  alsjdalkjd alksjd sa"
                                    Style="{StaticResource LabelValueStyle}" />

                                <Label
                                    Grid.Row="0"
                                    Grid.Column="1"
                                    Text="Primary Inspector"
                                    Style="{StaticResource LabelKeyStyle}" />

                                <Label
                                    Grid.Row="1"
                                    Grid.Column="1"
                                    Style="{StaticResource LabelValueStyle}" >
                                    <Label.FormattedText>
                                        <FormattedString>
                                            <Span Text="{Binding Path=InspectorFirstName}"/>
                                            <Span Text=" "/>
                                            <Span Text="{Binding Path=InspectorLastName}"/>
                                        </FormattedString>
                                    </Label.FormattedText>
                                </Label>
                            </Grid>

                            <Grid Grid.Row="2"
                                    Grid.Column="0"
                                    Grid.ColumnSpan="2"
                                    RowDefinitions="*, *"
                                    ColumnDefinitions="*, *">

                                <Label
                                    Grid.Row="0"
                                    Grid.Column="0"
                                    Text="Scheduled Date"
                                    Style="{StaticResource LabelKeyStyle}" />

                                <Label
                                    Grid.Row="1"
                                    Grid.Column="0"
                                    Text="{Binding Path=ScheduledStartDate, Converter={StaticResource dateFormatter},ConverterParameter='long'}"
                                    Style="{StaticResource LabelValueStyle}" />

                                <Label
                                    Grid.Row="0"
                                    Grid.Column="1"
                                    Text="Completed Date"
                                    Style="{StaticResource LabelKeyStyle}" />

                                <Label
                                    Grid.Row="1"
                                    Grid.Column="1"
                                    Text="{Binding Path=CompletionDate, Converter={StaticResource dateFormatter},ConverterParameter='long'}"
                                    Style="{StaticResource LabelValueStyle}" />
                            </Grid>
                        </Grid>
                    </Frame>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
        <StackLayout
                Grid.Row="1"
                VerticalOptions="End"
                HorizontalOptions="FillAndExpand"
                BackgroundColor="{StaticResource White}">
            <controlTemplate:BeginInspectionContentView></controlTemplate:BeginInspectionContentView>
        </StackLayout>
    </Grid>
</StackLayout>

Output & expected output image attached here: enter image description here

How to achieve dynamic height for this UI? Any help or suggestions are welcome.

CodePudding user response:

There are three solutions to this problem:

  1. You can change CollectionView to FlexLayout with BindableLayout.ItemsSource and BindableLayout.ItemTemplate. Then you don't need to set Height for item or FlexLayout because all are dynamic.
  2. You can set a value to collectionView.HeightRequest using MVVM. When item increase, the height will increase as well. For more details about this solution, you may refer to the link.
  3. You can use CollectionView.Behaviors to calculate the Height of every item to increase the height of ContentView. For more details about this solution, you may refer to the link.

CodePudding user response:

You have Grid. With StackLayout. With Grid. With Small Grids inside it.

I will put aside for now, that this is extremely bad for rendering. (It is serious problem however...)

None of the expected output justify this organization.

If you need for element to occupy 2 columns, you can use columns span 2. Before anything else, please combine all 4 grids in a single grid.

After you are done with that, limit your VisualElements height, or, even better, limit the content you are putting in them. (Because cut content looks bad. You should be using something like "show more" link, that opens popup or whatever).

If you have any questions, please ask.

  • Related