Xamarin.Forms: Use converters with binding objects which can have null as value.
You have a converter, which should return a specific value, even if the binded object is null.
Converters does not get executed, when the binded value is null. But here is a solution, how to handle this situations.
Define an example converter
In this case we will use an Int value to bool value converter.
public class IsIntGreaterThanConverter : IValueConverter, IMarkupExtension
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int bindedValue && parameter is int targetValue)
{
return bindedValue > targetValue;
}
else
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
This code returns true, if the binded value is greater than the value provided as a converter parameter.
It can be used with the following code snippet in XAML:
<Label Text="The value is greater than 0">
<Label.IsVisible>
<Binding Path="Object.IntValue"
Converter="{app1:IsIntGreaterThanConverter}">
<Binding.ConverterParameter>
<x:Int32>0</x:Int32>
</Binding.ConverterParameter>
</Binding>
</Label.IsVisible>
</Label>
But what happends, when the ‘Object’ is null? Well the converter does not get executed. And since View’s default value of the IsVisible bindable property is true, then the label will be visible, even if the Object is null.
How to handle null scenarios?
Bindings have a property called FallbackValue. Give a value to the fallbackvalue property in order to override the default value, like this:
<Label Text="The value is greater than 0">
<Label.IsVisible>
<Binding Path="Object.IntValue"
FallbackValue="False"
Converter="{app1:IsIntGreaterThanConverter}">
<Binding.ConverterParameter>
<x:Int32>0</x:Int32>
</Binding.ConverterParameter>
</Binding>
</Label.IsVisible>
</Label>
This should return false even if the binded obejct is null. 🙂
Tags In
1 Comment
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
![](https://www.banditoth.net/wp-content/uploads/2024/10/image.png)
Hi, I am András,
I am a seasoned software engineer from Budapest, Hungary with a strong focus on mobile app development using .NET MAUI and Xamarin.Forms. My expertise also extends to website building for my happy customers and other complex system designing. I am passionate about developing well-organized, maintainable software solutions.
![](https://www.banditoth.net/wp-content/uploads/2022/08/Screenshot-2022-08-29-at-23.49.40-1024x349.png)
![](https://www.banditoth.net/wp-content/uploads/2022/08/68747470733a2f2f7777772e706c616e657478616d6172696e2e636f6d2f436f6e74656e742f696d672f706c616e657478616d6172696e2d66656174757265642d62616467652e706e67.png)
[…] Xamarin.Forms: Use converters with binding objects which can have null as value. (András Tóth) Introduction to .NET MAUI (BogusÅ‚aw BÅ‚oÅ„ski) Exploring the Calendar Types in WinUI Scheduler (Jeyasri Murugan) Exploring the code behind MauiAppBuilder (Luis Matos) Invoke platform code in .NET MAUI (David Britch) […]