L_or_Enzo
L_or_Enzo

Reputation: 33

Limiting Label's width

So basically what i have is a Frame, with a Horizontal StackLayout which has 2 Labels in it:

(Label has colored background, Frame is white)

enter image description here

My problem is, when i have a longer name label, the price label doesn't get enough space and gets cut:

enter image description here

Which property should i change in order to not cut price label?

Here is the portion of code that creates the frame:

Frame elementFrame = new Frame {
    BackgroundColor = Xamarin.Forms.Color.White,
    Padding = 20,
};

StackLayout elementStackLayout = new StackLayout { 
    Orientation = StackOrientation.Horizontal,
    HorizontalOptions = LayoutOptions.Fill,
    BackgroundColor = Xamarin.Forms.Color.Orange
};

Label elementName = new Label {
    BackgroundColor = Xamarin.Forms.Color.Aqua,
    Text = FinalProduct.GetAttribute("Nome"),
    HorizontalOptions = LayoutOptions.Start,
};

Label elementPrice = new Label {
     BackgroundColor = Xamarin.Forms.Color.Aqua,
     LineBreakMode = LineBreakMode.NoWrap,
     HorizontalOptions = LayoutOptions.EndAndExpand,
     Text = "€" + ( Decimal.Parse(FinalProduct.GetAttribute("Prezzo")) * Int16.Parse(FinalProduct.GetAttribute("Quantita")) ).ToString("0.00"),
     HorizontalTextAlignment = TextAlignment.End
};

elementStackLayout.Children.Add(elementName);
elementStackLayout.Children.Add(elementPrice);
elementFrame.Content = elementStackLayout;

I tried messing around with HorizontalOptions, Label's LineBreakMode but with no success.

Upvotes: 0

Views: 260

Answers (1)

ToolmakerSteve
ToolmakerSteve

Reputation: 21223

StackLayout lacks a way to tell which item to "squeeze", unless you use a "hardcoded number" for both WidthRequest and MinimumWidthRequest on elementPrice.

Grid has a solution for this case, "Auto" vs "*":

<Grid ColumnDefinitions="*,Auto" HorizontalOptions="Fill">
  <Label Grid.Column="0" ... />
  <Label Grid.Column="1" x:Name="elementPrice" ... />
</Grid>

"Auto" in Column "1" will be given all the width it wants.
"*" in Column "0" gets what is left.

Upvotes: 1

Related Questions