András Tóth‘s professional blog
banditoth.net

Hey there 👋, I’m banditoth a .NET MAUI developer from Hungary.
I write about software development with .NET technologies.

You can find me on:
LinkedIn | Github | StackOverflow | X / Twitter | Threads

Xamarin.Forms: Use converters with binding objects which can have null as value.

This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

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. 🙂

One response to “Xamarin.Forms: Use converters with binding objects which can have null as value.”

  1. […] 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) […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.