hamalaiv
hamalaiv

Reputation: 858

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

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

Upvotes: 17

Views: 11432

Answers (4)

linktheory
linktheory

Reputation: 439

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>

Upvotes: 21

RANJITH KUMAR
RANJITH KUMAR

Reputation: 11

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;
        }
  }

Upvotes: 1

LeRoy
LeRoy

Reputation: 4436

Its a bug with Xmarin Forms here

Upvotes: 1

Ziyad Godil
Ziyad Godil

Reputation: 2680

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"

Upvotes: 4

Related Questions