Álvaro García
Álvaro García

Reputation: 19396

How could I set the length of a line to stretch in a MAUI application?

I would like to use a line in a MAUI project, and this is the documentation:

https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/shapes/line?view=net-maui-7.0

But i would like, in an horizontal line, to set that automatically the line use all the space from left to right. I don't see stretch options like another controls.

I could set a very long length, for example 10000, but I would like to know if there is a better way.

Thanks.

Upvotes: 4

Views: 2962

Answers (3)

Ben
Ben

Reputation: 2995

A Line is a Shape with the Aspect property of type Stretch.

To stretch a Line to fill the space of its layout, below a Grid row with default horizontal layout Fill, use the Aspect value Uniform:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="10"/>
    <RowDefinition Height="10"/>
  </Grid.RowDefinitions>

  <Line Grid.Row="0" Stroke="Red" X1="0" X2="100" Aspect="Uniform" />

  <Line Grid.Row="1" Stroke="Green" X1="0" X2="100" Aspect="None" />

</Grid>

Upvotes: 1

Luke Vo
Luke Vo

Reputation: 20728

To answer directly your question: since X2 is bindable, you can bind it to the width of itself:

<Line Stroke="Red" X2="{Binding Width, Source={RelativeSource Self}}" HorizontalOptions="Fill" StrokeThickness="3" />

enter image description here

My advice: MS has been using BoxView or BorderView for lines since forever, I guess it's better performance-wise as well. You should use the other solution instead unless you absolutely have to use Line.

Upvotes: 10

Umesh Kamble
Umesh Kamble

Reputation: 81

try the below code for horizontal line

<Style x:Key="HorizontalBoarder" TargetType="BoxView">
    <Setter Property="BackgroundColor" Value="White" />
    <Setter Property="HeightRequest" Value="1" />
    <Setter Property="HorizontalOptions" Value="FillAndExpand" />
</Style>
 <BoxView
         Margin="0,5"
            Style="{StaticResource HorizontalBoarder}" />

Upvotes: 0

Related Questions