Trigger Class

Definition

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

public ref class Trigger sealed : Microsoft::Maui::Controls::TriggerBase, Microsoft::Maui::Controls::Xaml::IValueProvider
[Microsoft.Maui.Controls.ContentProperty("Setters")]
[Microsoft.Maui.Controls.Xaml.AcceptEmptyServiceProvider]
public sealed class Trigger : Microsoft.Maui.Controls.TriggerBase, Microsoft.Maui.Controls.Xaml.IValueProvider
[<Microsoft.Maui.Controls.ContentProperty("Setters")>]
[<Microsoft.Maui.Controls.Xaml.AcceptEmptyServiceProvider>]
type Trigger = class
    inherit TriggerBase
    interface IValueProvider
Public NotInheritable Class Trigger
Inherits TriggerBase
Implements 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 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)

Initializes a new Trigger instance.

Properties

BindingContext

Gets or sets an object that contains the properties that will be targeted by the bound properties that belong to this BindableObject. This is a bindable property.

(Inherited from BindableObject)
Dispatcher

Gets the dispatcher that was available when this bindable object was created, otherwise tries to find the nearest available dispatcher (probably the window's/app's).

(Inherited from BindableObject)
EnterActions

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

(Inherited from TriggerBase)
ExitActions

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

(Inherited from TriggerBase)
IsSealed

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

(Inherited from TriggerBase)
Property

Gets or sets the property whose value will be compared to Value to determine when to invoke the 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

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

(Inherited from TriggerBase)
Value

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

Methods

ApplyBindings()

Applies all the current bindings to BindingContext.

(Inherited from BindableObject)
ClearValue(BindableProperty)

Clears any value that is previously set for a bindable property.

(Inherited from BindableObject)
ClearValue(BindablePropertyKey)

Clears any value that is previously set for a bindable property, identified by its key.

(Inherited from BindableObject)
CoerceValue(BindableProperty)

Coerces the value of the specified bindable property. This is done by invoking BindableProperty.CoerceValueDelegate of the specified bindable property.

(Inherited from BindableObject)
CoerceValue(BindablePropertyKey)

Coerces the value of the specified bindable property. This is done by invoking BindableProperty.CoerceValueDelegate of the specified bindable property.

(Inherited from BindableObject)
GetValue(BindableProperty)

Returns the value that is contained in the given bindable property.

(Inherited from BindableObject)
IsSet(BindableProperty)

Determines whether or not a bindable property exists and has a value set.

(Inherited from BindableObject)
OnBindingContextChanged()

Raises the BindingContextChanged event.

(Inherited from BindableObject)
OnPropertyChanged(String)

Raises the PropertyChanged event.

(Inherited from BindableObject)
OnPropertyChanging(String)

Raises the PropertyChanging event.

(Inherited from BindableObject)
RemoveBinding(BindableProperty)

Removes a previously set binding from a bindable property.

(Inherited from BindableObject)
SetBinding(BindableProperty, BindingBase)

Assigns a binding to a bindable property.

(Inherited from BindableObject)
SetValue(BindableProperty, Object)

Sets the value of the specified bindable property.

(Inherited from BindableObject)
SetValue(BindablePropertyKey, Object)

Sets the value of the specified bindable property.

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

For internal use by the Microsoft.Maui.Controls platform.

(Inherited from BindableObject)
UnapplyBindings()

Removes all current bindings from the current context.

(Inherited from BindableObject)

Events

BindingContextChanged

Occurs when the value of the BindingContext property changes.

(Inherited from BindableObject)
PropertyChanged

Occurs when a property value changes.

(Inherited from BindableObject)
PropertyChanging

Occurs when a property value is changing.

(Inherited from BindableObject)

Explicit Interface Implementations

IDynamicResourceHandler.SetDynamicResource(BindableProperty, String) (Inherited from BindableObject)
IValueProvider.ProvideValue(IServiceProvider)

Extension Methods

GetPropertyIfSet<T>(BindableObject, BindableProperty, T)
SetAppTheme<T>(BindableObject, BindableProperty, T, T)
SetAppThemeColor(BindableObject, BindableProperty, Color, Color)
SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String)

Creates and applies a binding to a property.

Applies to

See also