首页 > 解决方案 > 选择所有文本时的 Xamarin 条目光标位置

问题描述

当我的输入控件聚焦时,我选择所有文本。当文本超出输入控件的宽度时,文本的开头将被截断 - 如下图所示:

开头被截断

但是,我希望尾部被截断,如下所示:

尾巴被截断

<Entry x:Name="entryA" Focused="entry_Focused"/>

private void entry_Focused(object sender, FocusEventArgs e)
{
    entryA.CursorPosition = 0;
    entryA.SelectionLength = entryItem.Text.Length;
}

标签: c#xamarinxamarin.forms

解决方案


通过使用Entry的PropertyChanged方法,可以在数据框中的数据发生变化时进行操作。

我写了一个小例子供大家参考:</p>

这是xaml代码:

<StackLayout>
    <Entry x:Name="entryA" WidthRequest="100" HorizontalOptions="CenterAndExpand" PropertyChanged="entryA_PropertyChanged"></Entry>
</StackLayout>

下面是后台代码:

public partial class MainPage : ContentPage
{
    public int len { get; set; }
    public MainPage()
    {
        InitializeComponent();
    }

    private void entryA_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var en = sender as Entry;
        if (en.Text != null && len != en.Text.Length) 
        {
            entryA.CursorPosition = 0;
            len = en.Text.Length;
        }
    }
}

推荐阅读