Triggers In Xamarin.Forms

Here, we will discuss the following three triggers used in XAML.

  • Property Trigger
  • Data Trigger
  • Multi Trigger

Property Trigger

Property trigger is a trigger that gets fired when a property of a specific element is set to a specific value.

Let’s go through its syntax and example.

XAML

  1. <Entry Placeholder="Enter Email">  
  2.       <Entry.Triggers>  
  3.           <Trigger TargetType="Entry" Property="IsFocused" Value="True">  
  4.               <Setter Property="BackgroundColor" Value="Black"/>  
  5.               <Setter Property="TextColor" Value="White"/>  
  6.           </Trigger>  
  7.       </Entry.Triggers>  
  8.   </Entry>  

Explanation

In this example, we made a trigger which gets fired when our entry gets focused. And after the trigger fires, it sets the background color and text color of the element. This is one of the simple examples of property trigger.

Main things to remember in this trigger are,

  • Target Type
    It is the control name to which we are going to use the trigger.

  • Property
    This is the property of the control that is monitored.

  • Value
    It is the value which fires the trigger when a monitored property is set to a specific value.

  • Setter
    Collection of setters are applied to control when a specified condition is met.

Output

The output shows two images. In the first image, our entry is not focused and in the second image, the entry is focused; this activates our trigger and the conditions are applied to it.

Triggers In Xamarin.Forms  Triggers In Xamarin.Forms

Data Trigger

Data trigger is based on the binding that comes from any property of element present in this page or a ViewModel of a page. It is similar to property trigger but property trigger is only specified to a single element and does not depend upon binding values from other elements.

Let’s go through its syntax and example.

 XAML

  1. <Entry Placeholder="Enter Email" Text="" x:Name="Email">  
  2.     <Entry.Triggers>  
  3.         <Trigger TargetType="Entry" Property="IsFocused" Value="True">  
  4.             <Setter Property="BackgroundColor" Value="Black"/>  
  5.             <Setter Property="TextColor" Value="White"/>  
  6.         </Trigger>  
  7.     </Entry.Triggers>  
  8. </Entry>  
  9.   
  10. <Button Text="Submit">  
  11.     <Button.Triggers>  
  12.         <DataTrigger TargetType="Button" Binding="{Binding Source={x:Reference Email}, Path=Text.Length}" Value="0">  
  13.             <Setter Property="IsEnabled" Value="False"/>  
  14.             <Setter Property="BackgroundColor" Value="Red"/>  
  15.         </DataTrigger>  
  16.     </Button.Triggers>  
  17. </Button>  

Explanation

In this example, we applied the data trigger on a button. This trigger fires when the text length of the email entry is equal to zero. And to fire this trigger at start, we set the entry text to empty. So now, when a user writes any text in the entry, the button turns back to the default values. Our trigger will activate when the text length is zero which turns the "Submit" button into disabled state.

The additional thing in the data trigger to remember is binding. This binding came from an element or ViewModel.

Output

The output shows two images. In our first image, the text length is zero and in the second image, there is some text written. The first image activates our trigger and conditions are applied on the "Submit" button.

Triggers In Xamarin.Forms 
 
Triggers In Xamarin.Forms 

Multi Trigger

Multi trigger is the same as data trigger but in this trigger, we can write multiple conditions and our trigger activates when all conditions are true. So, there is an AND condition in our Multi Trigger. 

In simple words, if (condition1 && condition2 && condition3) {// Trigger setters}

Let’s go through its syntax and example.

XAML

  1. <Label Text="verify that you are not a robot:"/>  
  2. <Label TextColor="DarkBlue" Text="AzUf09"/>  
  3. <Entry Text="" x:Name="verficationEntry"/>  
  4.   
  5. <Label Text="Not Verified" TextColor="Red">  
  6.     <Label.Triggers>  
  7.         <MultiTrigger TargetType="Label">  
  8.             <MultiTrigger.Conditions>  
  9.                 <BindingCondition Binding="{Binding Source={x:Reference verficationEntry}, Path=Text.Length}" Value="6"/>  
  10.                 <BindingCondition Binding="{Binding Source={x:Reference verficationEntry}, Path=Text}" Value="AzUf09"/>  
  11.             </MultiTrigger.Conditions>  
  12.             <Setter Property="Text" Value="Verified"/>  
  13.             <Setter Property="TextColor" Value="Green"/>  
  14.         </MultiTrigger>  
  15.     </Label.Triggers>  
  16. </Label>  

Explanation

Here, we made a simple and useful example of multi trigger. This example tells you that you are verified when all conditions in multi trigger are satisfied. We made a simple check through this multi trigger.

Output

The output shows two images to explain the conditions of our multi trigger.

Triggers In Xamarin.Forms
 
Triggers In Xamarin.Forms


Similar Articles