How to check object null value in xamarin forms data trigger?
Asked Answered
D

4

17

I'm trying to check if a binding object value is null in Xamarin Forms XAML DataTrigger but I can't get it to work. I have tried the following:

<StackLayout IsVisible="True">
    <StackLayout.Triggers>
        <DataTrigger TargetType="StackLayout"
                        Binding="{Binding MyObject}"
                        Value="{x:Null}">
            <Setter Property="IsVisible" Value="False"></Setter>
        </DataTrigger>
    </StackLayout.Triggers>

    ...

</StackLayout>

Does anyone know a way to do it? I have tested this only on Android.

Edit: I have filed a bug report to xamarin bugzilla https://bugzilla.xamarin.com/show_bug.cgi?id=57863

Deuno answered 29/6, 2017 at 11:10 Comment(4)
Are you getting any errors? Do other bindings work correctly, i.e. is it definitely trying to check for null that is the issue?Jugulate
I don't see any errors and yes other bindings are working correctly.Deuno
I've just tested it and also get the same issue...it may be a Xamarin bug. One workaround would be to use a Converter that implements IValueConverter?Jugulate
Thanks for testing it. Converter might be a good workaround. I have filed a bug to xamarin bugzilla: bugzilla.xamarin.com/show_bug.cgi?id=57863Deuno
B
21

I know this is an old thread, but here is the solution:

Btw, you wouldn't need Isvisible="True" in the StackLayout because the default value is true.

<StackLayout IsVisible="True">
    <StackLayout.Triggers>
        <DataTrigger TargetType="StackLayout"
                        Binding="{Binding MyObject, TargetNullValue=''}"
                        Value="">
            <Setter Property="IsVisible" Value="False"></Setter>
        </DataTrigger>
    </StackLayout.Triggers>

    ...

</StackLayout>
Burtburta answered 12/6, 2020 at 3:27 Comment(4)
FYI - TargetNullValue='' seems to break in Xamarin Forms 5+ when I'm trying to use it on a Label.Now
@Now are you seeing a specific error or is it just not working as expected?Burtburta
I get a NullReferenceException when using TargetNullValue=''. Instead of using "empty" I changed to a zero and it worked for me.Burbank
I had to use FallbackValue instead of TargetNullValue to make it work.Glaucescent
M
4

You can use converter and set to it its work for me. Lets try below code.

Converter Code

public class NullValueBoolConverter: IValueConverter, IMarkupExtension
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            if (value is string)
            {
                if (string.IsNullOrEmpty(value as string))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {

                if (value == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
    }

And bind with IsVisible property like below :

<StackLayout IsVisible="{Binding Registerclosure.Notes, Converter={Helpers:NullValueBoolConverter}}">
</StackLayout>

Don't Forgot below line in header

xmlns:Helpers="clr-namespace:MyNameSpace"

Mciver answered 29/9, 2017 at 7:39 Comment(1)
converter will not be called if binding is null , this is made by xamarin designEmbrocate
I
1

Its a bug with Xmarin Forms here

Icy answered 13/11, 2017 at 12:1 Comment(1)
Yes I filed it. I have edited the question now to include the bugreport link.Deuno
T
1

Just optimizing the code in previous solution

  public class NullValueBoolConverter : IValueConverter, IMarkupExtension
  {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string)
            {
                string val = value as string;
                return !string.IsNullOrEmpty(val);
            }

            return !(value == null);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
  }
Toneytong answered 8/8, 2019 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.