Trigger Trigger Class

Definition

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

[Xamarin.Forms.ContentProperty("Setters")]
[Xamarin.Forms.Xaml.AcceptEmptyServiceProvider]
public sealed class Trigger : Xamarin.Forms.TriggerBase, Xamarin.Forms.Xaml.IValueProvider
type Trigger = class
    inherit TriggerBase
    interface IValueProvider
Inheritance
Attributes
Implements

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 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:

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.

<?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>

Constructors

Trigger(Type) Trigger(Type)

Initializes a new Trigger instance.

Properties

BindingContext BindingContext

Gets or sets object that contains the properties that will be targeted by the bound properties that belong to this BindableObject.

(Inherited from BindableObject)
EnterActions EnterActions

Gets the list of Xamarin.Forms.TirggerAction objects that will be invoked when the trigger condition is met. Ignored for the EventTrigger class.

(Inherited from TriggerBase)
ExitActions ExitActions

Gets the list of Xamarin.Forms.TirggerAction objects that will be invoked after the trigger condition is no longer met. Ignored for the EventTrigger class.

(Inherited from TriggerBase)
IsSealed IsSealed

Gets a value that indicates whether or not the trigger is sealed.

(Inherited from TriggerBase)
Property Property

Gets or sets the property whose value will be compared to Value to determine when to invoke the setters.

Setters Setters

Gets the list of Setter objects that will be applied when the property that is named by Property becomes equal to Value.

TargetType TargetType

The type of object to which this TriggerBase object can be attached.

(Inherited from TriggerBase)
Value Value

Gets or sets the value of the property, named by the Property property, that will cause the setters to be applied.

Explicit Interface Implementations

IAttachedObject.AttachTo(BindableObject) IAttachedObject.AttachTo(BindableObject)

For internal use by the Xamarin.Forms platform.

(Inherited from TriggerBase)
IAttachedObject.DetachFrom(BindableObject) IAttachedObject.DetachFrom(BindableObject)

For internal use by the Xamarin.Forms platform.

(Inherited from TriggerBase)
IDynamicResourceHandler.SetDynamicResource(BindableProperty, String) IDynamicResourceHandler.SetDynamicResource(BindableProperty, String)

For internal use by the Xamarin.Forms platform.

(Inherited from BindableObject)
IValueProvider.ProvideValue(IServiceProvider) IValueProvider.ProvideValue(IServiceProvider)

For internal use by the Xamarin.Forms platform.

Methods

ApplyBindings() ApplyBindings()

Apply the bindings to BindingContext.

(Inherited from BindableObject)
ClearValue(BindableProperty) ClearValue(BindableProperty)

Clears any value set by Xamarin.Forms.BindableObject.SetValue for property.

(Inherited from BindableObject)
ClearValue(BindablePropertyKey) ClearValue(BindablePropertyKey)

Clears any value set by Xamarin.Forms.BindableObject.SetValue for the property that is identified by propertyKey.

(Inherited from BindableObject)
GetValue(BindableProperty) GetValue(BindableProperty)

Returns the value that is contained the BindableProperty.

(Inherited from BindableObject)
GetValues(BindableProperty, BindableProperty) GetValues(BindableProperty, BindableProperty)

For internal use by the Xamarin.Forms platform.

(Inherited from BindableObject)
GetValues(BindableProperty, BindableProperty, BindableProperty) GetValues(BindableProperty, BindableProperty, BindableProperty)

For internal use by the Xamarin.Forms platform.

(Inherited from BindableObject)
IsSet(BindableProperty) IsSet(BindableProperty)

Returns true if the target property exists and has been set.

(Inherited from BindableObject)
OnBindingContextChanged() OnBindingContextChanged()

Override this method to execute an action when the BindingContext changes.

(Inherited from BindableObject)
OnPropertyChanged(String) OnPropertyChanged(String)

Call this method from a child class to notify that a change happened on a property.

(Inherited from BindableObject)
OnPropertyChanging(String) OnPropertyChanging(String)

Call this method from a child class to notify that a change is going to happen on a property.

(Inherited from BindableObject)
RemoveBinding(BindableProperty) RemoveBinding(BindableProperty)

Removes a previously set binding.

(Inherited from BindableObject)
SetBinding(BindableProperty, BindingBase) SetBinding(BindableProperty, BindingBase)

Assigns a binding to a property.

(Inherited from BindableObject)
SetValue(BindableProperty, Object) SetValue(BindableProperty, Object)

Sets the value of the specified property.

(Inherited from BindableObject)
SetValue(BindablePropertyKey, Object) SetValue(BindablePropertyKey, Object)

Sets the value of the propertyKey.

(Inherited from BindableObject)
SetValueCore(BindableProperty, Object, SetValueFlags) SetValueCore(BindableProperty, Object, SetValueFlags)

For internal use by the Xamarin.Forms platform.

(Inherited from BindableObject)
UnapplyBindings() UnapplyBindings()

Unapplies all previously set bindings.

(Inherited from BindableObject)

Events

BindingContextChanged BindingContextChanged

Raised whenever the BindingContext property changes.

(Inherited from BindableObject)
PropertyChanged PropertyChanged

Raised when a property has changed.

(Inherited from BindableObject)
PropertyChanging PropertyChanging

Raised when a property is about to change.

(Inherited from BindableObject)

Extension Methods

SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String) SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String)

Creates and applies a binding to a property.

SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String) SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String)

Creates and applies a binding from an expression.

GetGroup<TView,TItem>(TItem) GetGroup<TView,TItem>(TItem)

For internal use by platform renderers.

GetGroupHeaderContent<TView,TItem>(TItem) GetGroupHeaderContent<TView,TItem>(TItem)

For internal use by platform renderers.

GetIndex<TView,TItem>(TItem) GetIndex<TView,TItem>(TItem)

For internal use by platform renderers.

GetIsGroupHeader<TView,TItem>(TItem) GetIsGroupHeader<TView,TItem>(TItem)

For internal use by platform renderers.

SetIsGroupHeader<TView,TItem>(TItem, Boolean) SetIsGroupHeader<TView,TItem>(TItem, Boolean)

For internal use by platform renderers.

LoadFromXaml<TXaml>(TXaml, String) LoadFromXaml<TXaml>(TXaml, String)

Returns an initialized view by loading the specified xaml.

LoadFromXaml<TXaml>(TXaml, Type) LoadFromXaml<TXaml>(TXaml, Type)

Returns a TXaml with the properties that are defined in the application manifest for callingType.

Applies to

See Also