How do you get a box to fully expand to its container's width without WidthRequest="9999999"?

Daniel Donnelly, Jr 86 Reputation points
2023-01-06T19:43:34.887+00:00

I have tried HorizontalOptions="Fill/FillAndExpand/CenterAndExpand", and the grid that this VerticalStackLayout box sits in has a * or Auto size. No options seem to work exception WidthRequest="999999". Then it automatically sizes to its container's content width. Its container is a Grid inside of a Border box.

Developer technologies | .NET | .NET MAUI
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 60,346 Reputation points
    2023-01-06T19:46:54.033+00:00

    Setting the fill option should work. I suspect that the parent is itself configured to take up enough space to contain its children. As such you are telling a control to be as big as its parent and the parent to be large enough to fit the children. But I'm just guessing based upon the layout. Setting an absolute width of course would solve this issue since the child is now a fixed width and the parent resizes accordingly.

    Please provide the XAML that replicates the problem. If you have any custom code that is changing the widths of any of the controls then that would be useful as well.

    1 person found this answer helpful.
    0 comments No comments

  2. Daniel Donnelly, Jr 86 Reputation points
    2023-01-06T19:51:24.263+00:00

    @Michael Taylor

    <CollectionView.ItemTemplate>  
        <DataTemplate x:DataType="model:Device">  
            <Grid Padding="10" HorizontalOptions="Center">  
                <Border HorizontalOptions="CenterAndExpand" HeightRequest="140" Style="{StaticResource CardView}"  
                        StrokeShape="RoundRectangle 16">  
                    <Border.GestureRecognizers>  
                        <TapGestureRecognizer x:Name="avatarTapGesture"  
                            Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:DeviceListViewModel}}, Path=GoToDetailsCommand}"  
                            CommandParameter="{Binding .}"/>  
                    </Border.GestureRecognizers>  
                    <Grid Padding="0" ColumnDefinitions="128,Auto">  
                        <Border Style="{StaticResource CardView}"  
                                Stroke="{Binding ., Converter={StaticResource deviceToStatusColor}}"  
                                BackgroundColor="{Binding ., Converter={StaticResource deviceToStatusColor}}"  
                                StrokeShape="RoundRectangle 64"  
                                StrokeThickness="7"  
                                WidthRequest="128"  
                                HeightRequest="128"  
                                HorizontalOptions="Center"  
                                VerticalOptions="Center"  
                                Grid.Column="0">  
                            <Image  
                                Aspect="AspectFill"  
                                Source="{Binding Image}" />  
                        </Border>  
                        <VerticalStackLayout  
                            HorizontalOptions="Center"  
                            VerticalOptions="Center"  
                            WidthRequest="9999999"  
                            Grid.Column="1"  
                            Padding="10">  
                            <Label Style="{StaticResource LargeLabel}" Text="{Binding Name}" />  
                        </VerticalStackLayout>  
                    </Grid>  
                </Border>  
            </Grid>  
        </DataTemplate>  
    </CollectionView.ItemTemplate>  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.