Home > Back-end >  MAUI make button resize to fit text
MAUI make button resize to fit text

Time:11-08

How can i make a button resize itself to fit the entire text. Currently i have a CollectionView that gets populated with buttons with text from a collection. The issue i have is that the button does not expand to fit the text and for all buttons generated, the width is identical. Is it possible to make each button size itself just to fit in the text within it?

<CollectionView x:Name="TagsView"
                    Grid.Row="0">
        <CollectionView.ItemsLayout>
            <GridItemsLayout Orientation="Horizontal" />
        </CollectionView.ItemsLayout>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Button Text="{Binding Name}"
                        CornerRadius="15" 
                        HorizontalOptions="CenterAndExpand"/>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

What it looks like at the moment:

What it looks like at the moment

Thanks

CodePudding user response:

Firstly, if you want to make a button resize itself to fit the entire text, you need to set propeties HorizontalOptions and VerticalOptions of this button like below:

 HorizontalOptions="FillAndExpand"  VerticalOptions="CenterAndExpand"

And then,you should add a LinearItemsLayout with Horizontal Orientation like below:

<CollectionView.ItemsLayout>
    <LinearItemsLayout Orientation="Horizontal" />
</CollectionView.ItemsLayout>
  • Related