I have a scenario where I have several Entry controls that often have text that overflows what the entry control can show. I am trying to keep the text left aligned so I am using HorizontalTextAlighnment="Start" and it will display properly when IsReadOnly="False" but if I set the Entry to IsReadOnly="True" the text in the entry field will right align.
Here is a simple xaml page demonstrating the issue:
<?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="MauiBugExample.MainPage">
<ScrollView>
<VerticalStackLayout x:Name="PageVerticalStackLayout"
Padding="30,0"
Spacing="25">
<Label Text="HorizontalTextAlignment=Start IsReadonly=False" />
<Entry
HorizontalTextAlignment="Start"
HorizontalOptions="FillAndExpand"
Text="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
IsReadOnly="False"/>
<Label Text="HorizontalTextAlignment=Start IsReadonly=True" />
<Entry
HorizontalTextAlignment="Start"
HorizontalOptions="FillAndExpand"
Text="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
IsReadOnly="True"/>
</VerticalStackLayout>
</ScrollView>
This is what the output looks like. The first line where it is not read only is correctly left aligned where the second line that is readonly is right aligned when it should be left aligned:
Issue only presents if the text overflows the size of the control. If I have less text in the entry field it will stay left aligned even if ReadOnly=True
You can add a custom mapping to EntryHandler
to set the cursor position to the start whenever the Entry
is read-only.
EntryHandler.Mapper.AppendToMapping("CustomPosition", (h, v) =>
{
#if ANDROID
if (v.IsReadOnly)
{
h.PlatformView.SetSelection(0);
}
#endif
});
Add this during app startup, for example, in MauiProgram.cs
.