3

FlexLayout adjusts the spacing between the rows of content so the whole of its area is filled:

enter image description here

Can it be set to have no spacing between rows, or a fixed one? So that the rows would be packed to the top of the FlexLayout, and below there would be unused space? I have tried different settings (AlignItems, VerticalOptions, AlignContent) but couldn't achieve the desired result...

enter image description here

P.S.: the elements' size is fixed. These are added programmaticaly. XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp3.ActivitiesPage">
    <Grid RowDefinitions="50,*,75" ColumnDefinitions="200, *">
        <ScrollView Grid.RowSpan="2" x:Name="ActScroll">
            <VerticalStackLayout x:Name="Activities" Padding="5" />
        </ScrollView>
        <Frame Padding="5" Grid.Column="1">
            <Entry x:Name="ActText" BackgroundColor="Grey" />
        </Frame>
        <FlexLayout Grid.Row="1" Grid.Column="1" x:Name="Picker" Direction="Row" Wrap="Wrap" Padding="5" />
        <HorizontalStackLayout Grid.ColumnSpan="2" Grid.Row="2" Padding="5" >
            <Button x:Name="btnAdd" Text="Add" Clicked="btnAdd_Clicked" Margin="10"/>
            <Button x:Name="btnSave" Text="Save" Margin="10" IsEnabled="False" Clicked="btnSave_Clicked"/>
            <Button x:Name="btnDelete" Text="Delete" Margin="10" IsEnabled="False" Clicked="btnDelete_Clicked"/>
        </HorizontalStackLayout>
    </Grid>
</ContentPage>

Code behind (fragment):

    public ActivitiesPage()
{
    InitializeComponent();
    //color picker
    var ActColors = new Color[]
    {
        Colors.DimGray,
        Colors.DarkGray,
        Colors.Linen,
        Colors.AntiqueWhite,
        Colors.PeachPuff,
        Colors.Tan,
        Colors.BurlyWood,
        Colors.SandyBrown,
        Colors.Peru,
        Colors.Chocolate,
        Colors.Firebrick,
        Colors.DarkRed,
        Colors.Brown,
        Colors.IndianRed,
        Colors.LightCoral,
        Colors.LightPink,
        Colors.PaleVioletRed,
        Colors.HotPink,
        Colors.DeepPink,
        Colors.MediumVioletRed,
        Colors.DarkMagenta,
        Colors.DarkOrchid,
        Colors.Orchid,
        Colors.MediumPurple,
        Colors.BlueViolet,
        Colors.DarkSlateBlue,
        Colors.RoyalBlue,
        Colors.DodgerBlue,
        Colors.DeepSkyBlue,
        Colors.LightSkyBlue,
        Colors.PaleTurquoise,
        Colors.Turquoise,
        Colors.MediumTurquoise,
        Colors.Aqua,
        Colors.LightSeaGreen,
        Colors.SeaGreen,
        Colors.DarkGreen,
        Colors.ForestGreen,
        Colors.LimeGreen,
        Colors.PaleGreen,
        Colors.YellowGreen,
        Colors.Olive,
        Colors.DarkGoldenrod,
        Colors.DarkOrange,
        Colors.Gold,
        Colors.Orange,
        Colors.OrangeRed,
        Colors.Crimson,
        Colors.DarkSalmon,
        Colors.Coral,
    };

    foreach (var col in ActColors)
    {
        btn.Margin = 2;
        btn.MinimumWidthRequest = 50;
        btn.MaximumWidthRequest = 50;
        btn.MaximumHeightRequest = 50;
        btn.MinimumHeightRequest= 50;
        btn.FontSize = 12;
        btn.CornerRadius = 2;
        Picker.Add(btn);
    }
}
5
  • XAML goes a long way, when you ask about interface. (I mean, people will start telling you use Align, use Grow, etc... and you will say "It does not work", and the problem will be somewhere else) Commented Jan 26, 2023 at 11:26
  • @H.A.H. there's nothing special, just a FlexLayout and buttons are added into it programmatically. One peculiarity, the buttons' min/max width/height is set to 50. Commented Jan 26, 2023 at 11:31
  • AlignItems. Try it. Commented Jan 26, 2023 at 11:38
  • @H.A.H. didnt work Commented Jan 26, 2023 at 11:38
  • @StefanoMartini setting AlignContent="Start" makes the content disappear Commented Jan 26, 2023 at 11:43

1 Answer 1

3

(this is why, source code goes a long way, when you post question)

Your FlexLayout is solved by:

AlignContent="Start" AlignItems="Start" Direction="Row" Wrap="Wrap"

There is nothing else that is needed to have what you want.

The interesting part is the code that is around this FlexLayout. How will you make sure that it gets its desired Width and Height? Easy way is to set its Width and Height request.

(Good way to write interface, is to set faint background of containers, to watch its borders when you make changes.)

Edit: Tested it:

<FlexLayout x:Name="flex" AlignContent="Start" AlignItems="Start" Direction="Row" Wrap="Wrap" BackgroundColor="AliceBlue" HorizontalOptions="FillAndExpand" HeightRequest="500">

And populated it:

for (int i = 0; i < 100; i++)
    {
        Button b = new Button();
        b.WidthRequest = 25;
        b.HeightRequest = 25;
        b.Text = i.ToString();
        flex.Add(b);
    }

Everything is exactly like in your picture.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! So it requires both AlignContent and AlignItems set to "Start", seems i havent tried this exact combination.
Yes, you say, I want Rows, I want items from left to right, and rows from top to bottom. And when you reach the end, wrap.

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.