Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

IntegerToBooleanConverter

Mark Smith edited this page Aug 29, 2016 · 1 revision

IntegerToBooleanConverter

The IntegerToBooleanConverter value converter takes a System.Int32 input and turns it into a boolean based on the value of the integer and the setting of three properties.

This value converter can also be created inline with the associated binding as it implements IMarkupExtension. This is convenient if you are only using one of them in your page/app. However, if you need to reuse the converter, make sure to put it into your resources so that only a single copy is instantiated.

Properties

  • ZeroOrNull : the boolean value to return if the Integer is zero, null, or invalid. Defaults to false.
  • Positive : the boolean value to return if the Integer is > 0. Defaults to true.
  • 'Negative: the boolean value to return if the Integer is < 0. Defaults tofalse`.

Example

In this XAML example, the Label won't be displayed if the ListView has items.

<Page xmlns:cvt="clr-namespace:XamarinUniversity.Converters;assembly=XamU.Infrastructure">
   <Page.Resources>
      <ResourceDictionary>
         <cvt:IntegerToBooleanConverter x:Key="CountToVisibility" ZeroOrNull="true" Positive="false" />
      </ResourceDictionary>
   <Page.Resources>
<ListView x:Name="favorites" ... />

<Label Text="No Favorites" 
       IsVisible="{Binding Count, 
                      Source={x:Reference favorites}, 
                      Converter={StaticResource CountToVisibility}}" />