Quick Start Tutorial: Creating Universal Apps Via Xamarin: IValueConverter In Xaml - Part 14

Read the previous parts of the series here,

This article explains about the IValueConverter concept in XAML.

IValueConverter

In the binding, sometimes the source object can’t directly assign to the target object. The source object is different in the target object. In the code behind the page, we can easily implement the conversion method. What about XAML? It is an IValueConverter interface.

IValueConverter Interface

The main purpose of this interface is to convert the source object to the target object (Oneway Binding) and the target to the source object (sometimes, we need target to have a source object in Twoway or OneWayToSource binding).

Flow of the IValueConverter
Flow
The target object does not directly bind the object. It goes to the convert function to do the conversion, based on the source type and assigns to the source object. The source object is also the same, but it calls the convertBack method.

Example: Xamarin button control

In the button, IsVisible is a Boolean property, which when set to be true is visible to GUI and false is hidden to make this more readable in the code behind the page like Visible or Hidden instead of true or false. In the background of Xaml, it needs to be only true or false.

Let us see how to implement this concept. 
  1. Define the enum in the code behind the page,
    1. public enum Visibility  
    2. {  
    3.    Hidden = 0,  
    4.    Visible = 1  
    5. }  
  2. Create a variable for the visibility of enum.
    1. private Visibility _btnVisibility;  
    2.         public Visibility BtnVisibility  
    3.         {  
    4.             get { return _btnVisibility; }  
    5.             set  
    6.             {  
    7.                 _btnVisibility = value;  
    8.                 OnPropertyChanged();  
    9.             }  
    10.         }  
  3. Implement in the IValueConverter and Convert enum to convert the function (object direction target to the source object),
    1. class VisibilityBool : IValueConverter  
    2.     {  
    3.         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
    4.         {  
    5.             //Convert enum to bool  
    6.             var  visible = (Visibility) value;  
    7.             return visible != Visibility.Hidden;  
    8.         }  
    9.   
    10.         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
    11.         {  
    12.             return null;  
    13.         }  
    14.     }  
  4. In the Xaml, define this VisibilityBool class

    xmlns:local="clr-namespace:PickerDemo;assembly=PickerDemo"

  5. Add this class into the ResourceDictionary
    1. <ContentPage.Resources>  
    2.    <ResourceDictionary>  
    3.      <local:VisibilityBool x:Key="VisibleBool"/>  
    4.    </ResourceDictionary>  
    5.  </ContentPage.Resources> 
  6. Implement this binding with the converter function and define with the key (conversation class, which is defined in the resource dictionary) into the IsVisible property.
    1. <StackLayout>  
    2.     <Button x:Name="BtnValueConverter"   
    3.             Text="ValueConverter"  
    4.             IsVisible="{Binding BtnVisibility,Converter={StaticResource VisibleBool}}"  
    5.             VerticalOptions="Center"   
    6.             HorizontalOptions="Center">  
    7.     </Button>  
    8.   </StackLayout>  
  7. Code behind the page assigns the Visible or Hidden 
  8. BtnVisibility = Visibility.Hidden; // Button control is Hidden.

That’s it, in all the code files we can use the enum visibility instead of true or false, and assigning to Xaml, internally it calls the convert function which changes as true or false based on enum and updates to Button IsVisible property.

Note: Don’t forget to assign the BindingContext property

Tip

To declare the IValueConverter class in Xaml into the resource dictionary, instead of directly assigning the class in the button IsVisible, make use of the property itself.

  1. <Button x:Name="BtnValueConverter" Text="ValueConverter"  
  2.       <Button.IsVisible>  
  3.         <Binding Path="BtnVisibility">  
  4.           <Binding.Converter>  
  5.             <local:VisibilityBool /> <!--assign IValueConverter class here-->  
  6.           </Binding.Converter>  
  7.         </Binding>  
  8.       </Button.IsVisible>  
  9.  </Button>  
Binding: ConverterParameter

This is a parameter used to pass the arguments of the convert and ConvertBack parameters

Example IValueConverter 
output

Up Next
    Ebook Download
    View all
    Learn
    View all