Prevent a Border control from shrinking when its child controls shrink

Viewed 34

I have a Border control that contains a couple of Label controls. The text shown in the labels changes, and this causes the Border control to expand and shrink horizontally. To prevent flickering, I want it to be able to expand but not shrink. Is there a way to do this?

Alternatively, is there a way to specify that the Label objects should expand horizontally but never shrink - which would give the same outcome?

My XAML is:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="uk.andyjohnson.TakeoutExtractor.Gui.ProgressOverlay">
    <Border
        Stroke="#0f0f0f"
        StrokeThickness="2"
        StrokeShape="RoundRectangle 5,5,5,5"
        HorizontalOptions="CenterAndExpand"
        VerticalOptions="CenterAndExpand"
        Padding="30,30">
        <VerticalStackLayout
            Spacing="25" >

            <Label 
                x:Name="SourceLabel"
                Text=""
                HorizontalOptions="Center"/>
            <Label 
                x:Name="DestinationLabel"
                Text=""
                HorizontalOptions="Center" />
        </VerticalStackLayout>
    </Border>
</ContentView>

Thanks!

2 Answers

You could play with labels WidthRequest. When your label increase in size, you set its WidthRequest to its current Width value.

Assuming for simplicity that you're changing the label text in your code-behind and that there is only one label, something like this should do the job.

public partial class ProgressOverlay
{
     private double currentWidth = 0;

     // I create this method just to explain.
     // It could be anything in your code that changes your label text
     void ChangingText()
     {
         // ******
         // .... SourceLabel text changes here ...
         // *******

         if (SourceLabel.Width > currentWidth)
         {
             SourceLabel.WidthRequest = SourceLabel.Width;
             currentWidth = SourceLabel.Width;
         }
     }
}

I solved the problem using a variation on Riccardo Minato's suggestion.

  1. Hook into the SizeChanged event on the bounding Border object.
  2. Track its maximum width using a member variable
  3. When the width exceeds the previous maximum, set the Border object's MinimumWidthRequest property to the new maximum width. This stops it shrinking.

New XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="uk.andyjohnson.TakeoutExtractor.Gui.ProgressOverlay">
    <Border
        Stroke="#0f0f0f"
        StrokeThickness="2"
        StrokeShape="RoundRectangle 5,5,5,5"
        HorizontalOptions="CenterAndExpand"
        VerticalOptions="CenterAndExpand"
        Padding="30,30"
        SizeChanged="Border_SizeChanged">  <!-- ** Added this line ** -->
        <VerticalStackLayout
            Spacing="25" >
            <ActivityIndicator
                IsVisible="true"
                IsRunning="true"
                IsEnabled="true"
                HorizontalOptions="Center"/>
            <Button
                x:Name="CancelButton"
                Text="Cancel"
                HorizontalOptions="Center"
                Clicked="OnCancelButtonClicked"/>
            <Label 
                x:Name="SourceLabel"
                Text=""
                HorizontalOptions="Center"/>
            <Label 
                x:Name="DestinationLabel"
                Text=""
                HorizontalOptions="Center"/>
        </VerticalStackLayout>
    </Border>
</ContentView>

New code-behind:

private double currentMaxWidth = 0D;

private void Border_SizeChanged(object sender, EventArgs e)
{
    var bdr = sender as Border;
    if (bdr.Width > currentMaxWidth)
    {
        currentMaxWidth = bdr.Width;
        bdr.MinimumWidthRequest = currentMaxWidth;
    }
}
Related