Xamarin.Forms Guides | Samples

Xamarin.Forms.Trigger Class

Class that represents a property condition and an action that is performed when the condition is met.

See Also: Trigger

Syntax

[Xamarin.Forms.ContentProperty("Setters")]
[Xamarin.Forms.Xaml.AcceptEmptyServiceProvider]
public sealed class Trigger : TriggerBase, IValueProvider

See Also

Remarks

The Trigger class is suitable for checking the values of any property on the control to which it has been added. That is, its default binding context is the control to which it has been added. To bind on controls other than the parent, developers should use the DataTrigger class, instead.

The XML example below prompts the user to type in the secret, which is, "The text color is red". When the user has typed the secret, the Entry text changes color to red. The code is turned back to the default color if the user then edits the text to differ from the secret. This example shows how to bind to the enclosing control with a trigger.

XML Example


<?xml version="1.0" encoding="utf-8"?>
<ContentPage    xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:TriggerDemo"
                x:Class="TriggerDemo.TriggerDemoPage">
    <StackLayout VerticalOptions="Center">
        <Label Text="Tell me the secret"
               VerticalOptions="Center"
               HorizontalOptions="Center" />
        <Entry Placeholder="Type the secret here."
               VerticalOptions="Center"
               HorizontalOptions="Center">
            <Entry.Triggers>
                <Trigger TargetType="Entry"
                    Property="Text"
                    Value="The text color is red">
                    <Setter Property="TextColor" Value="Red" />
                </Trigger>
            </Entry.Triggers>
        </Entry>
    </StackLayout>
</ContentPage>

The example below shows how developers can use a trigger to respond to events and update the value of a control property by using Xamarin.Forms.TriggerAction<T> classes. The example prompts the user to answer a question about the color of the text, and then calls ColorTriggerAction to turn the text red when the user types "The text color is red". Developers should note that, while this example does not change the text back to the default color when the user continues to edit the string, the developer could additionally implement and specify an exit action to obtain that result.

With the ColorTriggerAction class below defined in the TriggerDemo namespace:

C# Example

public class ColorTriggerAction : TriggerAction<Entry>
{
    protected override void Invoke(Entry sender)
    {
        sender.TextColor = Color.Red;
    }
}

the developer can use the XAML below to create the page that responds to the secret string by changing the color of the Entry text area to red.

XAML Example

<?xml version="1.0" encoding="utf-8"?>
<ContentPage    xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:TriggerDemo"
                x:Class="TriggerDemo.TriggerDemoPage">
    <StackLayout VerticalOptions="Center">
        <Label Text="What color is &quot;The text&quot;?"
               VerticalOptions="Center"
               HorizontalOptions="Center" />
        <Entry Placeholder="Type answer here."
               VerticalOptions="Center"
               HorizontalOptions="Center"
               BackgroundColor="White">
            <Entry.Triggers>
                <Trigger TargetType="Entry"
                         Property="Text"
                         Value="The text is red" >
                    <Trigger.EnterActions>
                        <local:ColorTriggerAction />
                    </Trigger.EnterActions>
                </Trigger>
            </Entry.Triggers>
        </Entry>
    </StackLayout>
</ContentPage>

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.3.0.0, 1.4.0.0, 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.Trigger are listed below.

See Also: TriggerBase

Public Constructors

Initializes a new Trigger instance.

Public Properties

PropertyBindableProperty. Gets or sets the property whose value will be compared to Trigger.Value to determine when to invoke the setters.
[read-only]
SettersIList<Setter>. Gets the list of Setter objects that will be applied when the property that is named by Trigger.Property becomes equal to Trigger.Value.
ValueObject. Gets or sets the value of the property, named by the Trigger.Property property, that will cause the setters to be applied.

Explicitly Implemented Interface Members

Trigger$IValueProvider.ProvideValue(IServiceProvider)For internal use by the Xamarin.Forms platform.