Xamarin.Forms Guides | Samples

Xamarin.Forms.DataTrigger Class

Class that represents a condition on an arbitrary binding, along with a list of Setter objects that will be applied when the condition is met.

See Also: DataTrigger

Syntax

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

Remarks

The DataTrigger class is suitable for checking values on other controls, as well as 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.

The XML example below prompts the user to type in the secret, which is, "The text color is green". When the user has typed "The", the Entry text changes color to red. When the user completes the secret, the Label text turns green. The code is turned back to the default color if the user then edits the secret. This example shows how to bind to the enclosing control (Entry from Entry), and to an arbitary control (Entry from Label)

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="Can you tell me the secret?" VerticalOptions="Center" HorizontalOptions="Center">
                          <Label.Triggers>
                              <DataTrigger TargetType="Label"
                                  Binding="{Binding Source={x:Reference checkEntry},
                                                    Path=Text}"
                                  Value="The text color is green">
                                  <Setter Property="TextColor" Value="Color.Green" />
                              </DataTrigger>
                          </Label.Triggers>
                      </Label>
                      <Entry Placeholder="Type the secret here." x:Name="checkEntry" VerticalOptions="Center" HorizontalOptions="Center">
                          <Entry.Triggers>
                              <DataTrigger TargetType="Entry"
                                  Binding="{Binding Text}"
                                  Value="The">
                                  <Setter Property="TextColor" Value="Red" />
                              </DataTrigger>
                          </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.DataTrigger are listed below.

See Also: TriggerBase

Public Constructors

Initializes a new DataTrigger instance.

Public Properties

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

Explicitly Implemented Interface Members

DataTrigger$IValueProvider.ProvideValue(IServiceProvider)For internal use by the XAML infrastructure.