A data trigger changes a XAML property when bound data reaches a specified value. It uses a Setter, much like a style, and can watch a property in the binding context or on any control in the view. When its condition stops being true, the triggered change is reverted. section 10.2.1 section 10.4
Use a data trigger for immediate UI feedback instead of waiting for submission-time validation. For example, disable input that cannot yet be used: a Stepper remains unavailable until a product is selected, and an Add count button remains unavailable while Count is 0. section 10.2.1
Create one in the target control's Triggers collection:
- Choose DataTrigger and set TargetType to the target control type. section 10.2.1
- Bind Binding to the value that represents the condition, such as SelectedProduct or Count. section 10.2.1
- Set Value to the value that activates the trigger, then add a Setter for the property to change. section 10.2.1
- Provide a suitable default for custom bindable properties, because a trigger restores that default after its condition no longer matches. section 11.2.2
Disable controls until data is ready
Bind a Stepper trigger to SelectedProduct, using TargetNullValue='' and Value='' to recognize no selection, then set IsEnabled to False. A Button can similarly bind to Count with Value="0" and set IsEnabled to False. Selecting a product or entering a nonzero count restores each control's normal enabled state. section 10.2.1
Listing 10.11 The updated Stepper code in InputPage.xaml
<Stepper Grid.Row="3"
HorizontalOptions="Center"
VerticalOptions="Center"
Value="{Binding Count}">
<Stepper.Triggers> #1
<DataTrigger TargetType="Stepper" #2
Binding="{Binding SelectedProduct,
TargetNullValue=''}" #3
Value=""> #4
<Setter Property="IsEnabled" #5
Value="False" /> #6
</DataTrigger>
</Stepper.Triggers>
</Stepper>
This listing shows the complete Stepper pattern: a trigger bound to SelectedProduct sets IsEnabled to False when no product is selected. section 10.2.1
Data triggers differ from property triggers by watching external or binding-context data rather than a property on the target control. They also differ from event triggers: data-trigger changes revert when the condition is no longer satisfied, whereas event-trigger changes do not. section 10.2.1
Replace a missing date and show focus
A date Label can replace DateTime.Minimum with “No stock counted” when no count was recorded. A Border around an Entry can watch the Entry's IsFocused property and set StrokeThickness on Android, giving a focused field a visible outline. section 10.3.2 section 11.3.3
