Bug 57863 - Datatrigger not working with null value
Summary: Datatrigger not working with null value
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.5
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Stephane Delcroix
: 59644 ()
Depends on:
Reported: 2017-06-29 17:10 UTC by hamalainen.ville.p
Modified: 2018-01-04 08:10 UTC (History)
6 users (show)

Tags: xaml datatrigger bindableproperty
Is this bug a regression?: ---
Last known good build:

sample project (294.80 KB, application/x-zip-compressed)
2017-07-06 20:31 UTC, Jimmy [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 57863 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description hamalainen.ville.p 2017-06-29 17:10:06 UTC
Checking DataTrigger binding against null value does not work in Xamarin Forms XAML. I have tested the following code:

<StackLayout IsVisible="True">
        <DataTrigger TargetType="StackLayout"
                        Binding="{Binding MyObject}"
            <Setter Property="IsVisible" Value="False"></Setter>



I have made sure that "MyObject" is actually null. There are no errors happening, the Setter just does not fire. I have tested this bug only on Android.
Comment 1 Namyslaw Szymaniuk 2017-06-30 11:25:45 UTC
Just FYI - You can use *Converter* to achieve exact what you need in such:
<StackLayout IsVisible="{Binding MyObject, Converter={StaticResource VisibilityNotNullConverter}}">

public class VisibilityNotNullConverter : IValueConverter
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            return value != null;

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            throw new NotImplementedException();

For me, *DataTriggers* are just fked up in latest releases - https://bugzilla.xamarin.com/show_bug.cgi?id=56515
but not sure about your case.
Comment 2 Jimmy [MSFT] 2017-07-06 20:31:29 UTC
Created attachment 23362 [details]
sample project

I was not able to reproduce this with the sample project I am attaching. I tested with Forms and 2.3.5-pre6 on Android, iOS, and UWP and it worked on all platforms. 

If you are still experiencing this issue, can you attach a repro project so we can investigate further? When providing the requested information, please set the report's status back to NEW otherwise we may close this issue after 30 days of no response. Thanks!
Comment 3 hamalainen.ville.p 2017-07-07 16:55:26 UTC
Thanks Jimmy for testing this. I can reproduce the issue I have if I set the initial value of the binding object to null. Is it expected result that the DataTrigger does not fire on initial value?
Comment 4 胡玮文 2017-08-11 07:43:27 UTC
@Jimmy The problem is the initial value null is ignored. You can verify this by just change line 16 of MainPage.xaml.cs in your sample project to "null". And I don't think this is the expected behavior, because if I bind to an int property, the initial value is not ignored.
Comment 5 hamalainen.ville.p 2017-08-12 06:19:45 UTC
Is it expected that initial null value is ignored? (changed status back to NEW)
Comment 6 Jimmy [MSFT] 2017-08-15 17:11:22 UTC
Thanks for clarifying that! I am now also seeing the issue described when the initial value for the property is null. I will have the team look into this to verify, but at the moment I believe this is expected behavior. 

Because a BindableProperty will already be null when created if no other default value is provided, this means that there is no initial property change occurring and the DataTrigger does not fire. In this case, a better approach would be to set the StackLayout to not be visible by default and then use a ValueConverter as mentioned in comment 1 so you can determine if the property is _not_ null.

Hope this helps!
Comment 7 Paul DiPietro [MSFT] 2017-09-21 14:10:22 UTC
*** Bug 59644 has been marked as a duplicate of this bug. ***