Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.
This repository was archived by the owner on May 1, 2024. It is now read-only.

[Enhancement] add SetFocusOnEntryCompletedBehavior #841

@jmegner

Description

@jmegner

Summary

Add SetFocusOnEntryCompletedBehavior, which is a compact way of getting another control to get focus when user completes an entry.

See this blog post: https://adenearnshaw.com/focus-next-element-on-enter/

Just for reference, a bit of Twitter discussion where original author approves: https://twitter.com/jacobegner/status/1357767010206298112

API Changes

Add a Xamarin.CommunityToolkit.Behaviors.SetFocusOnEntryCompletedBehavior class.

Spec

public class SetFocusOnEntryCompletedBehavior : Behavior
{
    public static readonly BindableProperty TargetElementProperty
       = BindableProperty.Create(nameof(TargetElement), typeof(VisualElement), typeof(SetFocusOnEntryCompletedBehavior));

    public VisualElement TargetElement
    {
        get => (VisualElement)GetValue(TargetElementProperty);
        set => SetValue(TargetElementProperty, value);
    }
        
    protected override void OnAttachedTo(BindableObject bindable)
    {
        base.OnAttachedTo(bindable);
        
        if (bindable is Entry entry)
            entry.Completed += Entry_Completed;
    }

    protected override void OnDetachingFrom(BindableObject bindable)
    {
        if (bindable is Entry entry)
            entry.Completed -= Entry_Completed;

        base.OnDetachingFrom(bindable);
    }

    private void Entry_Completed(object sender, EventArgs e)
    {
        TargetElement?.Focus();
    }
}

Intended Use Case

It's great for a sequence of entries where completing one entry automatically jumps the user to the next entry, but it can jump focus to any control.

<ContentPage x:Class="NextFormFieldSample.Forms.MainPage"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:behaviors="clr-namespace:NextFormFieldSample.Forms.Behaviors">
    <StackLayout Margin="20">   
        
        <Entry Placeholder="Field 1">
            <Entry.Behaviors>
                <behaviors:SetFocusOnEntryCompletedBehavior TargetElement="{x:Reference Entry2}" />
            </Entry.Behaviors>
        </Entry>

        <Entry x:Name="Entry2" Placeholder="Field 2"/>  
        
    </StackLayout>
</ContentPage>

Who Will Do The Work?

  • I am willing to take this on myself
  • Just putting this out there for someone to pick up

Metadata

Metadata

Assignees

No one assigned

    Labels

    a/behaviorsThis issue/PR is related to behaviorsfeature-requestA request for a new feature.needs-approvalFeature request has been submitted but is awaiting final approval. Please do not implement before!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions