|
| 1 | +--- |
| 2 | +title: Setting Focus on Entry Using MVVM in UI for .NET MAUI |
| 3 | +description: Learn how to set focus on an Entry control when it loads using the MVVM approach in UI for .NET MAUI. |
| 4 | +type: how-to |
| 5 | +page_title: How to Set Focus on Entry Control in UI for .NET MAUI Using MVVM |
| 6 | +meta_title: Focus Entry Control Using MVVM in UI for .NET MAUI |
| 7 | +slug: set-focus-entry-mvvm-ui-net-maui |
| 8 | +tags: entry, mvvm, telerik ui for .net maui, entry focus, eventtocommandbehavior, radeventtocommandbehavior |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 11.1.0 | Telerik UI for .NET MAUI Entry | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to use `EventToCommandBehavior` for Entry focus in .NET MAUI? |
| 24 | +- How to use `RadEventToCommandBehavior` for Entry focus in .NET MAUI? |
| 25 | +- How to set Entry focus programmatically using MVVM? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To achieve this, use either the `EventToCommandBehavior` from CommunityToolkit or `RadEventToCommandBehavior` from Telerik UI for .NET MAUI. |
| 30 | + |
| 31 | +### Approach 1: Using `RadEventToCommandBehavior` |
| 32 | + |
| 33 | +This approach doesn't require explicitly setting the `BindingContext` inside the event to command behavior. |
| 34 | + |
| 35 | +1. Add the behavior to the Entry control: |
| 36 | + |
| 37 | + ```xaml |
| 38 | + <te:RadEntry.Behaviors> |
| 39 | + <te:RadEventToCommandBehavior |
| 40 | + EventName="Loaded" |
| 41 | + Command="{Binding EntryLoadedCommand}" |
| 42 | + CommandParameter="{Binding Source={x:Reference Entry}}" /> |
| 43 | + </te:RadEntry.Behaviors> |
| 44 | + ``` |
| 45 | + |
| 46 | +2. Create a command in your ViewModel: |
| 47 | + |
| 48 | + ```csharp |
| 49 | + [RelayCommand] |
| 50 | + async Task EntryLoaded(object sender) |
| 51 | + { |
| 52 | + if (sender is Telerik.Maui.Controls.RadEntry entry) |
| 53 | + { |
| 54 | + await MainThread.InvokeOnMainThreadAsync(() => entry.Focus()); |
| 55 | + } |
| 56 | + } |
| 57 | + ``` |
| 58 | + |
| 59 | +### Approach 2: Using `EventToCommandBehavior` from CommunityToolkit |
| 60 | + |
| 61 | +1. Explicitly set the behavior's `BindingContext` to the page's ViewModel: |
| 62 | + |
| 63 | + ```xaml |
| 64 | + <te:RadEntry.Behaviors> |
| 65 | + <toolkit:EventToCommandBehavior |
| 66 | + EventName="Loaded" |
| 67 | + Command="{Binding EntryLoadedCommand}" |
| 68 | + CommandParameter="{Binding Source={x:Reference Entry}}" |
| 69 | + BindingContext="{Binding Source={x:Reference MainPage}, Path=BindingContext}" /> |
| 70 | + </te:RadEntry.Behaviors> |
| 71 | + ``` |
| 72 | + |
| 73 | +2. Add a name to the ContentPage: |
| 74 | + |
| 75 | + ```xaml |
| 76 | + <ContentPage x:Name="MainPage" ...> |
| 77 | + ``` |
| 78 | + |
| 79 | +3. Define the command in your ViewModel: |
| 80 | + |
| 81 | + ```csharp |
| 82 | + [RelayCommand] |
| 83 | + async Task EntryLoaded(object sender) |
| 84 | + { |
| 85 | + if (sender is Telerik.Maui.Controls.RadEntry entry) |
| 86 | + { |
| 87 | + await MainThread.InvokeOnMainThreadAsync(() => entry.Focus()); |
| 88 | + } |
| 89 | + } |
| 90 | + ``` |
| 91 | + |
| 92 | +Both approaches achieve the desired behavior of setting focus on the Entry control when it loads. |
| 93 | + |
| 94 | +## See Also |
| 95 | + |
| 96 | +- [UI for .NET MAUI Entry Documentation](https://docs.telerik.com/devtools/maui/controls/entry/overview) |
| 97 | +- [RadEventToCommandBehavior Documentation](https://docs.telerik.com/devtools/maui/api/telerik.maui.controls.radeventtocommandbehavior) |
| 98 | +- [CommunityToolkit EventToCommandBehavior Documentation](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/behaviors/event-to-command-behavior) |
0 commit comments