How to add binding property to Label with converter BoolToObjectConverter

Didier Vera 1 Reputation point
2022-09-06T08:27:24.5+00:00

I've the next converter code like the example in this post Xamarin.Forms Binding Converter
but when the value come in method only have the property Path="DeviceHasScanner" and the Binding property never is called :(

public class BoolToObjectConverter<T> : IValueConverter  
    {  
       public T TrueObject { set; get; }  
  
       public T FalseObject { set; get; }  
  
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
       {  
           return (bool)value ? TrueObject : FalseObject;  
       }  
  
       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
       {  
           return ((T)value).Equals(TrueObject);  
       }  
   }  

And I'm try implement it on this way:

<Image HorizontalOptions="Center" WidthRequest="200">  
     <Image.Source>  
         <Binding Source="{Binding DeviceHasScanner}">  
               <Binding.Converter>  
                     <converters:BoolToObjectConverter x:TypeArguments="x:String"  TrueObject="presstoscan.png" FalseObject="scanwithcamera.png" />  
                </Binding.Converter>  
          </Binding>  
     </Image.Source>  
 </Image>  
  

And the boolean binding property in viewmodel is it but never is called:

public bool DeviceHasScanner  
    {  
        get   
        {  
            return Settings.Get.DeviceHasScanner; //This code return true or false  
        }  
 }  

What is the correct way to implement it?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,321 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points Microsoft Vendor
    2022-09-07T05:28:24.8+00:00

    Hello,

    The Value Convertor will get value from the property value of Path.

    For instance:

       <Binding Source="{RelativeSource Self}"  
                Path="WidthRequest">  
    

    If you set WidthRequest to Path, you will find the value of converter is 200.

    Therefore, you need to set a new property into Image to bind DeviceHasScanner as the following code:

       public class MyImage : Image  
       {  
           public static readonly BindableProperty DeviceHasScannerProperty =  
       BindableProperty.Create("DeviceHasScanner", typeof(bool), typeof(MyImage), null);  
           public bool DeviceHasScanner  
           {  
               get { return (bool)GetValue(DeviceHasScannerProperty); }  
               set { SetValue(DeviceHasScannerProperty, value); }  
           }  
       }  
    

    In XAML:

       <converters:MyImage x:Name="img" HorizontalOptions="Center" WidthRequest="200" DeviceHasScanner="{Binding DeviceHasScanner}">  
       ...  
       <Binding Source="{RelativeSource Self}"  
                Path="DeviceHasScanner">  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments