Home > Blockchain >  Get width of parent element in .Net maui
Get width of parent element in .Net maui

Time:10-01

I am trying to draw a Line in a VerticalStackLayout in .net Maui. I want the line to stretch from left to right.

Is there a way to get the size of the parent element? I tried it with the WidthRequest which results in nothing because it wasn't set.

   <Line X1="0"
            Y1="0"
            X2="{ Binding Source={RelativeSource TemplatedParent}, Path=WidthRequest}"
            Y2="0"
            StrokeLineCap="Round"
            Stroke="{StaticResource Icon}"
            StrokeThickness="1"
          AbsoluteLayout.LayoutBounds="70,0"
          BackgroundColor="Gray"/>

CodePudding user response:

The problem was, that the Line was in a VerticalStackLayout which was in an AbsoluteLayout and this won't pass its size down, which means that the HorizontalOptions and VerticalOptions have no effect on the VerticalStackLayout

My Solution is as follows:

Either the Line is in the AbsoluteLayout itself

<Line X1="0"
        Y1="0"
        X2="0"
        Y2="0"
        StrokeLineCap="Round"
        Stroke="{StaticResource Icon}"
        StrokeThickness="1"

        AbsoluteLayout.LayoutBounds="70,0,1,1"
        AbsoluteLayout.LayoutFlags="WidthProportional"
        BackgroundColor="Gray"/>

With the combination of AbsoluteLayout.LayoutBounds="70,0,1,1" and AbsoluteLayout.LayoutFlags="WidthProportional", the Line stretches from left to right.

Or the Line is in the VerticalStackLayout, which then needs to use the AbsoluteLayout.LayoutBounds

  <VerticalStackLayout BindableLayout.ItemsSource="{Binding Hours}"
                         AbsoluteLayout.LayoutFlags="WidthProportional"
                         AbsoluteLayout.LayoutBounds="0,0,1,1"
                         >

With the combination of AbsoluteLayout.LayoutFlags="WidthProportional" and AbsoluteLayout.LayoutBounds="0,0,1,1", the VerticalStackLayout stretches to the width of the AbsoluteLayout.

If my control wasn't in an AbsoluteLayout it would have worked as Leandro Toloza stated in the comment above.

  • Related