Triggers, Xamarin

Getting started with Triggers in Xamarin Forms

Sometimes, we need to make some actions when an event happens or when a control property is changed. So, to do it Triggers are the solutions! With Triggers, we can add one or more actions to our controls, based on what do you want to do, maybe to make an action when a click event happens in a Button or when an Entry control have been Focused. In this post, I will teach you how to do it! Let’s see!

What is a Trigger?

A Trigger, let you add actions to the graphic controls from the XAML, allowing to change the appearance of your controls when a specific action happens. This actions can be: When an event happens or when a property control is changed.

There are four types of triggers:

1⃣  Property triggers

2⃣  Data triggers 

3⃣  Event triggers

4⃣  Multi-triggers

What are one of the main terms I need to know?

To declare a Trigger, you might know the following triggers parts:

🔹 TargetType     Is the control type that will be getting the trigger. For example: Button.

🔹 Property         Is the property that is observed by the Trigger to be triggered. For example: Is Focused.

🔹 Value              Is the value that must have the Property mentioned above. This value next to the Property will be observed by the Trigger to be triggered. For example: Is   Focused = True.

🔹 Setter       Sets the action that will get the control when the trigger is activated. Here the Property and Value must be established too. For example: Add a blue Background color. ( I show some examples bellow).

Let’s learn type by type

Property triggers

Property triggers are used when a specific property change happens by another existing property and value.

Here the example showed above

In this example the trigger from an Entry control is activated when is Focused. The action triggered just changes the Entry Text and the BackGroundColor properties.

Data triggers

The behavior is similar to the Property Triggers with the difference that Data Triggers checks the property from a specific control to apply the Setter established in other control. To check it, this trigger type, uses the Binding property. To get the Binding property you have to use the x:Reference followed on the control name. (In this case, Entry name).

Here the example showed above

In this example the trigger from the Button control is activated when the TextEntry has the length property in zero. The action triggered just change the Button Text and the BackGroundColor properties.

Event triggers

Event triggers are activated when an event happens in our control.

How to do it?

1⃣ Step one: You have to create a class and inherit from the TriggerAction class and you must indicate the control type to which it will be applied. After this, you will override the Invoke method and inside this method, you will add whatever you want to do with the control.

2⃣ Step two: Add the Event trigger and call the class created previously.

Here the example showed above

In this example the trigger from the Button control is activated when the Click event happens. The action triggered will be determine from the ChangeColorclass (Created previously).

Multi triggers

Multi triggers have a similar behavior as the Data Triggers. But, in this types of Triggers you will be able to add more than one condition and to activate it,  all the conditions must be given.

Here the example showed above

In this example the trigger from theButton control is activated when the TextEntry and FullNameEntrEntries have the length property in six and the IsFocused property is True, respectively. The action triggered just change the Button Text and the BackGroundColor properties.

Spanish post:



Tagged , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *