New to Telerik UI for .NET MAUIStart a free 30-day trial

Setting Focus on Entry Using MVVM in UI for .NET MAUI

Updated on Sep 26, 2025

Environment

VersionProductAuthor
11.1.0Telerik UI for .NET MAUI EntryDobrinka Yordanova

Description

I want to set focus on an Entry control when the page loads using the MVVM (Model-View-ViewModel) approach in UI for .NET MAUI.

This knowledge base article also answers the following questions:

  • How to use EventToCommandBehavior for Entry focus in .NET MAUI?
  • How to use RadEventToCommandBehavior for Entry focus in .NET MAUI?
  • How to set Entry focus programmatically using MVVM?

Solution

To achieve this, use either the EventToCommandBehavior from CommunityToolkit or RadEventToCommandBehavior from Telerik UI for .NET MAUI.

Approach 1: Using RadEventToCommandBehavior

This approach doesn't require explicitly setting the BindingContext inside the event to command behavior.

  1. Add the behavior to the Entry control:

    xaml
    <te:RadEntry.Behaviors>
        <te:RadEventToCommandBehavior
                EventName="Loaded"
                Command="{Binding EntryLoadedCommand}"
                CommandParameter="{Binding Source={x:Reference Entry}}" />
    </te:RadEntry.Behaviors>
  2. Create a command in your ViewModel:

    csharp
    [RelayCommand]
    async Task EntryLoaded(object sender)
    {
        if (sender is Telerik.Maui.Controls.RadEntry entry)
        {
            await MainThread.InvokeOnMainThreadAsync(() => entry.Focus());
        }
    }

Approach 2: Using EventToCommandBehavior from CommunityToolkit

  1. Explicitly set the behavior's BindingContext to the page's ViewModel:

    xaml
    <te:RadEntry.Behaviors>
        <toolkit:EventToCommandBehavior
            EventName="Loaded"
            Command="{Binding EntryLoadedCommand}"
            CommandParameter="{Binding Source={x:Reference Entry}}"
            BindingContext="{Binding Source={x:Reference MainPage}, Path=BindingContext}" />
    </te:RadEntry.Behaviors>
  2. Add a name to the ContentPage:

    xaml
    <ContentPage x:Name="MainPage" ...>
  3. Define the command in your ViewModel:

    csharp
    [RelayCommand]
    async Task EntryLoaded(object sender)
    {
        if (sender is Telerik.Maui.Controls.RadEntry entry)
        {
            await MainThread.InvokeOnMainThreadAsync(() => entry.Focus());
        }
    }

Both approaches achieve the desired behavior of setting focus on the Entry control when it loads.

See Also