Fallbacks
There are occasions where data binding can fail; the binding source cannot be resolved (yet) or the returned value is null. Although ValueConverters and additional code could tackle many such situations, we can also enhance the resilience of our data bindings by setting fallback values. This can easily be done by setting the TargetNullValue
or FallbackValue
property in our binding expression.
TargetNullValue
The TargetNullValue
property can be used in situations where we want to handle the situation where the resolved binding source target returns null
. In other words, the binding engine can resolve the bound property, but this property returns a null
value.
In our app, the Calories
property on RecipeDetailViewModel
is defined as a nullable int
. This makes it essential for us to handle any potential null values in our data binding elegantly. If we leave the binding statement as-is, the label would show "Calories: kcal"
if the Calories
property is null
...