Try It Yourself
Practice what you learned in this chapter in an interactive code playground.
The interactive playground at the end of this lesson contains a .NET MAUI project that was set up with various types of triggers and action classes associated with them. We can apply the following modifications to it and see how they would affect the structure of the compiled application:
Applying a property trigger.
Adding a data trigger.
Applying an event trigger.
Applying a device state trigger.
Applying changes to the
EnterActions
andExitActions
triggers.
Applying a property trigger
In the setup at the end of this lesson, we have two Entry
elements. The second element doesn't have any triggers. We'll need to apply a property trigger to it that will set the Background
property of this element to Green
when focused.
Adding a data trigger
Next, we'll apply another trigger to the second Entry
element. The trigger will use the first Entry
element as its BindingSource
. To enable such a relationship, we would need to add the x:Name
attribute to the first Entry
element.
The data trigger will read the Length
property of the Text
field of the first Entry
element. If the first element is empty (i.e., if the Length
value is 0
)the second Entry
element should be disabled. This can be achieved by setting the IsEnabled
property to False
inside the trigger's Setter
.
Applying an event trigger
We'll now add another Entry
element and add an EventTrigger
element to it. We'll associate the trigger with the TextChanged
event. The ChangeTextColorAction
will represent the trigger action.
Applying a device state trigger
In the MainPage.xaml
file in the setup at the end of the lesson, we have orientation state triggers that will toggle between the horizontal and vertical alignment of the elements on the screen, depending on whether the screen is in the portrait or landscape orientation. Let's now change our original OrientationStateTrigger
to DeviceStateTrigger
. This is how it can be set:
Get hands-on with 1400+ tech skills courses.