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?
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.
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.
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.
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.
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
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.
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.
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.
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).
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With