question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] IsNull / IsNotNull Converters

See original GitHub issue

IsNull IsNotNull Converters

  • Proposed
  • Prototype
  • Implementation
    • iOS Support
    • Android Support
    • macOS Support
    • Windows Support
  • Unit Tests
  • Sample
  • Documentation: Done

Link to Discussion

Discussion

Summary

The IsNullConverter is a converter that allows users to convert an incoming binding to a bool value. This value represents if the incoming binding value is null.

The IsNotNullConverter is a converter that allows users to convert an incoming binding to a bool value. This value represents if the incoming binding value is not null.

Motivation

As mentioned in the discussion this was the behaviour of another converter, so these converters would just reimplement the old behaviour. It’s been quite common in the projects I’ve worked on to be able to check any object type if it’s null or not.

Detailed Design

IsNotNullConverter.shared.cs

/// <summary>
/// Converts the incoming value to a <see cref="bool"/> indicating whether or not the value is not null.
/// </summary>
public class IsNotNullConverter : BaseConverter<object?, bool>
{
	/// <summary>
	/// Converts the incoming object to a <see cref="bool"/> indicating whether or not the value is not null.
	/// </summary>
	/// <param name="value">The value to convert.</param>
	/// <returns>A <see cref="bool"/> indicating if the incoming value is not null</returns>
	public bool ConvertFrom(object? value) =>  value is not null;
}

IsNullConverter.shared.cs

/// <summary>
/// Converts the incoming value to a <see cref="bool"/> indicating whether or not the value is null.
/// </summary>
public class IsNullConverter : BaseConverter<object?, bool>
{
	/// <summary>
	/// Converts the incoming object to a <see cref="bool"/> indicating whether or not the value is not null.
	/// </summary>
	/// <param name="value">The value to convert.</param>
	/// <returns>A <see cref="bool"/> indicating if the incoming value is not null</returns>
	public bool ConvertFrom(object? value) =>  value is null;
}

Usage Syntax

XAML Usage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="MyLittleApp.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <xct: IsNullConverter x:Key="IsNullConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout IsVisible="{Binding MyObject, Converter={StaticResource IsNullConverter}}">

    </StackLayout>
</ContentPage>

C# Usage

class MyPage : ContentPage
{
  public MyPage()
  {
    Content = new StackLayout().Bind(StackLayout.IsVisibleProperty, nameof(ViewModel.MyObject), converter: new IsNullConverter())
  }
}

Drawbacks

None

Alternatives

None

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
IeuanWalkercommented, Mar 22, 2022

Cool no prob, i’ll wait

1reaction
IeuanWalkercommented, Mar 16, 2022

@brminnick awesome, ye will do

Read more comments on GitHub >

github_iconTop Results From Across the Web

CommunityToolkit/docs/maui/converters/is-not-null- ...
The IsNotNullConverter is a converter that allows users to convert an incoming binding to a bool value. This value represents if the incoming ......
Read more >
is-string-not-null-or-empty-converter.md
The IsStringNotNullOrEmptyConverter is a one way converter that returns a bool indicating whether the binding value is not null and not an ...
Read more >
Deciding between COALESCE and ISNULL in SQL Server
When writing T-SQL, a lot of developers use either COALESCE or ISNULL in order to provide a default value in cases where the...
Read more >
DataTrigger where value is NOT null? - wpf
This is a bit of a cheat but I just set a default style and then overrode it using a DataTrigger if the...
Read more >
Performance implications of using ISNULL vs IS NULL
This query is looking for any records where the AccountNbr column is empty or NULL. From looking at the execution plan we'll see...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found