Xamarin Data Triggers

Data triggers for beginners.

Let’s say we want to show a label indicating that the person retires this year if their age is 65.

First, we have a Model in the code behind which has an age field. We set that to the BindingContext:

public HumanResourcesApp()
{
    InitializeComponent();
    BindingContext = new Model() {Age = 65};
    .....

Now in the XAML we add a trigger that says: “When the Age is = 65 change the IsVisible to true”

<Label Text="You retire this year" IsVisible="False">
   <Label.Triggers>
        <DataTrigger Binding="{Binding Age}" TargetType="Label" Value="65">
               <Setter Property="IsVisible" Value="true"></Setter>
        </DataTrigger>
    </Label.Triggers>
</Label>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s