0

I have an issue with GridItemsLayout and VerticalOptions="Fill" the element in DataTemplate doesn't fill the whole available space. Only if I set HeightRequest="60" the element has a height that I expect but I want to avoid hardcoded UI. This is my code and image how it looks like in my UI (the blue rectangle is the whole available space but items fixed like be in VerticalOptions="Start").

<Grid 
    RowDefinitions="*, 2.5*">
    <Label
        Padding="0, 0, 0, 3"
        Style="{StaticResource TextBlue5}"
        Text="Sample label"/>
    <CollectionView 
                Grid.Row="1"
                ItemsSource="{Binding Buttons}"
                VerticalOptions="Fill">
        <CollectionView.ItemsLayout>
            <GridItemsLayout Orientation="Vertical"
                         Span="6"
                         HorizontalItemSpacing="0"
                         VerticalItemSpacing="0"
                         />
        </CollectionView.ItemsLayout>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid RowDefinitions="*" VerticalOptions="Fill" HorizontalOptions="Fill">
                    <Border 
                        Stroke="{StaticResource Blue1}"
                        StrokeThickness="1" VerticalOptions="Fill" HorizontalOptions="Fill">
                        <Grid RowDefinitions="Auto"  VerticalOptions="Fill" HorizontalOptions="Fill" >
                            <ImageButton  Source="{Binding Source}"
                                 Command="{Binding Command}"
                                 Style="{StaticResource DefaultImageButtonFill}"
                            />
                            </Grid>
                    </Border>
                </Grid>   
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</Grid>

enter image description here

6
  • 1
    Can you add the code for the grid that the collection view is in? Commented Dec 6 at 9:39
  • I've edited. Please take a look now. Commented Dec 6 at 14:15
  • By default, a vertical GridItemsLayout will display items in a single column. However, you set the GridItemsLayout.Span property to 6. This results in a six-column grid. So, your image show three buttons taking up half the space (3 columns). Commented Dec 7 at 17:58
  • But I want these buttons to fill the blue rectangle in terms of height, not width. Commented Dec 7 at 18:08
  • Seems you're just using the CollectionView to position ImageButtons? If that's the only use case for the CollectionView, I'd be tempted to say you don't need it. You can either (1) create a subclass of Grid where the ImageButtons are being instantiated in your subclass, or (2) attach a BindableLayout to a Grid. Commented Dec 8 at 23:12

2 Answers 2

0

I managed this by using FlexLayout instead of CollectionView. So my layout now looks like:

<Grid 
    RowDefinitions="*, 2.5*">
    <Label 
        Padding="0, 0, 0, 3"
        Style="{StaticResource TextBlue5}"
        Text="Sample label"/>
    <FlexLayout
        Grid.Row="1"
        BindableLayout.ItemsSource="{Binding Buttons}"
        Direction="Row"
        Wrap="Wrap"
        JustifyContent="Start"
        AlignItems="Stretch">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <Border Stroke="{StaticResource Blue1}" StrokeThickness="1"
                        FlexLayout.Basis="16.66%">
                    <ImageButton
                        Source="{Binding Source}"
                        Command="{Binding ButtonCommand}"
                        CommandParameter="{Binding .}"
                    </ImageButton>
                </Border>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </FlexLayout>
</Grid>
Sign up to request clarification or add additional context in comments.

Comments

0

You have specified Span="6" it means that GridItemsLayout will create space for 6 items to fit, but in you have only 3 items inside your ItemsSource, thus rest of the space is empty.

Also you should not use FlexLayout since it has multitude of issues starting with concistancy and performance

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.