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

hamalaiv picture hamalaiv · Jun 29, 2017 · Viewed 7k times · Source

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

Answer

linktheory picture linktheory · Jun 12, 2020

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>