admin管理员组

文章数量:1297094

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=";
         xmlns:x=";
         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

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/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft/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

Share Improve this question asked Feb 11 at 17:50 LarryDevLarryDev 1301 silver badge6 bronze badges 2
  • A user is still allowed to interact with the Entry for copy and paste purposes, so, the cursor position can be anywhere and, the focus will be linked to the cursor position. However, does adding CursorPosition="0" help? – Stephen Quan Commented Feb 11 at 23:53
  • Thank you @StephenQuan for the suggestion but it did not work. The solution below adding the EntryHandler did however do the trick. – LarryDev Commented Feb 13 at 0:04
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: