Skip to content

Converters

Neil McAlister edited this page Feb 8, 2017 · 1 revision

Converters

Xalami includes a XAML Value Converter to make data binding simpler.

EmptyNullToBoolConverter

Converts a value that is empty or null to a Boolean. Null, empty or falsy values will evaluate to false, and non-null, and truthy values will evaluate to true.

  • Null is considered falsy
  • Default values for primitive types are considered falsy
  • A null or whitespace-only string is considered falsy
  • An empty or null IEnumerable is considered falsy.

Usage

Import the Converters namespace, then use it as you would any other value converter:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Xalami.Views"
             x:Class="Xalami.Views.MainPage"
             xmlns:conv="clr-namespace:Xalami.Converters">
    <ContentPage.Resources>
        <ResourceDictionary>
            <conv:EmptyNullToBoolConverter x:Key="EmptyNullToBoolConverter"/>
        </ResourceDictionary>
    </ContentPage.Resources>
    
    <ListView ItemsSource="{Binding YourListSource}"
              IsVisible="{Binding YourListSource, Converter={StaticResource EmptyNullToBoolConverter}}"/>

</ContentPage>

And like any other value converter, you can declare it in your App.xaml if you want to be able to refer to it from any page without declaring it on that page.

Clone this wiki locally