Home > Net >  Stretch element to bottom of page
Stretch element to bottom of page

Time:08-08

I'm working on a .NET MAUI app. One of the pages is a chat screen. I'm using a TableView to log the messages with an entry element under it for the user to send messages.

The problem is that when too many chat messages are sent, the entry is no longer visible because the TableView has stretched beyond the page boundary. What I'd like to do is have the tablearea continue to expand until it and the entry hits the bottom, allowing the user to scroll through the chat messages and having the entry at the bottom of the screen.

Here's my current XAML.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.ChatView"
             Title="ChatView">
    <VerticalStackLayout>
        <TableView Intent="Data" VerticalOptions="Fill">
            <TableRoot>
                <TableSection x:Name="chatTable"/>
            </TableRoot>
        </TableView>
        <Entry Placeholder="Chat" x:Name="chatInput" VerticalOptions="End" Keyboard="Chat"/>
    </VerticalStackLayout>
</ContentPage>

The chat messages are then dynamically inserted into the chatTable with C#.

CodePudding user response:

At first you can try to use the Grid and ScrollView, such as:

<Grid >
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
        <ScrollView x:Name="scrollview" Grid.Row="0">
        <TableView Intent="Data" VerticalOptions="Fill">
        <TableRoot>
            <TableSection x:Name="chatTable"/>
        </TableRoot>
        </TableView>
        </ScrollView>
        <Entry Grid.Row="1" Placeholder="Chat" x:Name="chatInput" VerticalOptions="End" Keyboard="Chat"/>
</Grid>

In addition, you can also try to use the scroll view to contain the TableView. Such as:

<VerticalStackLayout >
        <ScrollView x:Name="scrollview">
        <TableView Intent="Data" VerticalOptions="Fill">
        <TableRoot>
            <TableSection x:Name="chatTable"/>
        </TableRoot>
        </TableView>
        </ScrollView>
        <Entry Placeholder="Chat" x:Name="chatInput" VerticalOptions="End" Keyboard="Chat"/>
       
    </VerticalStackLayout>

Finally, you can try to use the scrollview to contain the VerticalStackLayout and scroll to the bottom when the srollview's size changes.

<ScrollView x:Name="scrollview">
    <VerticalStackLayout >
        <TableView Intent="Data" VerticalOptions="Fill">
        <TableRoot>
            <TableSection x:Name="chatTable"/>
        </TableRoot>
        </TableView>
        <Entry Placeholder="Chat" x:Name="chatInput" VerticalOptions="End" Keyboard="Chat"/>
    </VerticalStackLayout>
</ScrollView>

Page.cs:

scrollview.SizeChanged  = (s, e) =>
    {
        scrollview.ScrollToAsync(chatInput, ScrollToPosition.End, true);
    };
  • Related