Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a command to Entry "Unfocused" event in Xamarin Forms

I have a command (or a piece of code) which I want to run when a specific entry loses its focus.

I'm using MVVM so I want to do it using the binding way instead of writing code in my Form.xaml.cs.

How can I make my command (or my piece of code) run when my entry loses its focus within XAML file?

like image 540
mehrandvd Avatar asked Jan 30 '19 07:01

mehrandvd


People also ask

How do you bind in code behind xamarin forms?

The binding references the source object. To set the data binding, use the following two members of the target class: The BindingContext property specifies the source object. The SetBinding method specifies the target property and source property.

How does binding work in xamarin forms?

Data binding is the technique of linking properties of two objects so that changes in one property are automatically reflected in the other property. Data binding is an integral part of the Model-View-ViewModel (MVVM) application architecture.

What is two way binding in xamarin forms?

However, the default binding mode for the Value property of Slider is TwoWay . This means that when the Value property is a data-binding target, then the target is set from the source (as usual) but the source is also set from the target. This is what allows the Slider to be set from the initial Opacity value.

How do I set focus in xamarin?

You can set the focus programmatically to the editor in Xamarin. Forms SfDataForm by customizing the existing editor. Refer to the online user guide documentation for creating new custom editor in DataForm. Set focus to view on loading in OnInitializeView method using Focus method.

What is unfocused event in Xamarin forms?

Unfocused event is raised whenever the VisualElement loses focus. This event is not bubbled through the Xamarin.Forms stack and is received directly from the native control. This event is emitted by the IsFocusedProperty setter. In the context of commanding, behaviors are a useful approach for connecting a control to a command

What is entry in Xamarin forms?

Xamarin.Forms Entry. The Xamarin.Forms Entry is used for single-line text input. The Entry, like the Editor view, supports multiple keyboard types. Additionally, the Entry can be used as a password field.

How do I use single line text in Xamarin forms?

Single-line text or password input. The Xamarin.Forms Entry is used for single-line text input. The Entry, like the Editor view, supports multiple keyboard types. Additionally, the Entry can be used as a password field. The Entry, like other text-presenting views, exposes the Text property.

How to add focused and unfocused event handlers to a grid?

Add 2 Entry Controls in Grid Assign Focused and Unfocused event handlers to them (I do this with lamda expressions) First tap on first Entry, then on second Expected behavior is to trigger only Focused event when you tap on Entry.


2 Answers

There is an example of an EventToCommandBehavior in the Xamarin.Forms samples (see here). Using this you could bind your control

<Entry>
    <Entry.Behaviors>
        <behaviors:EventToCommandBehavior 
            EventName="Unfocused" 
            Command="{Binding EntryUnfocused}" />
    </Entry.Behaviors>
</Entry>

Then define EntryUnfocused in your class of viewmodel.cs file of your particular view, like below:

public class LoginViewModel : XamarinViewModel
{
    public ICommand EntryUnfocused{ get; protected set; } 
    public LoginViewModel()
    {
        EntryUnfocused= new Command(CompletedCommandExecutedAsync);
    }

    private void CompletedCommandExecutedAsync(object param)
    {
     //yourcode...
    }
}

If you are using the Prism library, you can use their implementation, which is a bit more mature (allowing you to map the event arguments by specifying, which parameter shall be passed), see here.

(Please note that you'll have to add the respective namespace the behavior lives in to your XAML file).

like image 182
Paul Kertscher Avatar answered Dec 03 '22 09:12

Paul Kertscher


If you use Xamarin Community Toolkit, you can use EventToCommandBehavior which is already there.

Using this will be the same as in the Paul Kertscher answer.

Add to your control header:

xmlns:toolkit="http://xamarin.com/schemas/2020/toolkit"

In your Entry bind to the command:

<Entry.Behaviors>
    <toolkit:EventToCommandBehavior
        EventName="Unfocused"
        Command="{Binding EntryUnfocused}" />
</Entry.Behaviors>
like image 24
jizai Avatar answered Dec 03 '22 08:12

jizai