Binding a converter to a Custom ViewCell

StevenSajjaStevenSajja USMember, University
edited July 2015 in Xamarin.Forms

Greetings Guys.

I am trying to bind a label to an uppercase converter I have created. I don't know what I am doing wrong. (New bee issues)

Below is my Converter
public class CaseConverter : IValueConverter
{

    public enum TextCase
    {
        Upper = 0,
        Lower = 1
    }
    public CaseConverter(){}

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var returnValue = value as string;

        TextCase textCase;
        textCase = (TextCase)value;

        switch (textCase) {
        case TextCase.Lower:
            returnValue = returnValue.ToLower ();
            break;
        case TextCase.Upper:
            returnValue.ToUpper ();
            break;
        default:
            returnValue.ToString();
            break;
        }
        return returnValue;

    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

And Below is my ViewCell.

<?xml version="1.0" encoding="UTF-8"?>

<ViewCell.BindingContext>
    <local:HomeViewModel/>
    <helpers:CaseConverter x:Key="UpperCaseConverter"/>             
</ViewCell.BindingContext>

<ViewCell.View>
    <StackLayout Orientation="Horizontal" BackgroundColor="White" Padding="10" Spacing="20">
        <StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand">
            <Label Text="{Binding Title}" FontSize="Medium" TextColor="#34495e"/>
            <Label Text="{Binding Category, Converter={StaticResource UpperCaseConverter}}" FontSize="Small" TextColor="#34495e"/>
            <Label Text="{Binding Description}" FontSize="Medium" TextColor="#000000"/>
            <StackLayout Orientation="Horizontal">
                <Label Text="{Binding PubDate}" HorizontalOptions="Start" TextColor="#000000"/>
                <Label Text="{Binding Creator}" HorizontalOptions="End" TextColor="#000000"/>
            </StackLayout>
        </StackLayout>
    </StackLayout>
</ViewCell.View>

The error I get is Position 9:7. Property BindingContext is null or is not IEnumerable

Can anybody help?

Thanks

Answers

  • DanielLDanielL PLInsider ✭✭✭✭
    edited July 2015
    TextCase textCase;
    textCase = (TextCase)value;
    

    Your value is a string. You cannot cast it to TextCase. If you want to have a converter with options (eg. lower or upper). You should pass it through converter constructor.

    But I don't think it's possible to use converters with non default constructors in XAML. So try to do a separate IValueConverter for each option:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var returnValue = value as string;
            return returnValue.ToLower();
        }
    

    EDIT: Actually you can do it in XAML with default constructor and using ConverterParameter in XAML:

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var returnValue = value as string;
                var param = parameter as string;
    
                if (param == "ToUpper")
                    return returnValue.ToUpper();
    
                else if (param == "ToLower")
                    return returnValue.ToLower();
    
                return returnValue;
            }
    

    XAML:
    <Label Text="{Binding Category, Converter={StaticResource UpperCaseConverter}, ConverterParameter='ToUpper'}" FontSize="Small" TextColor="#34495e"/>

  • StevenSajjaStevenSajja USMember, University
    edited July 2015

    Hey Daniel,

    Thanks for your response. I now believe that my problem is how do I have a Static Resource in a Custom ViewCell?
    With your help my converter is correct but no code beyond the constructor is hit as I cant access the converter.

  • DanielLDanielL PLInsider ✭✭✭✭
    <ViewCell.BindingContext>
        <local:HomeViewModel/>
        <helpers:CaseConverter x:Key="UpperCaseConverter"/>             
    </ViewCell.BindingContext>
    

    You can't have more than one context here. Why is CaseConverter there? It should be there:

    <ContentPage.Resources>
        <ResourceDictionary>
          <local:CaseConverter x:Key="caseConverter" />
        </ResourceDictionary>
      </ContentPage.Resources> 
    
Sign In or Register to comment.