Using a converter with Background colour

NinjaFocksNinjaFocks USMember ✭✭
edited December 2016 in Xamarin.Forms

I'm trying to use a converter to change the background colour of a stack layout based on a type which is picked up from a binding. The stack layout is in a list and each item has a type attribute which is a string.

Here is my xaml;
<StackLayout HorizontalOptions="FillAndExpand" BackgroundColor="{Binding Type, Converter={StaticResource backgroundColour}}">

and this is my converter;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { switch (value.ToString().ToUpper()) { case "type1": return "#008334"; case "type2": return "#FFFFFF"; case "type3": return "#dedddd"; case "type4": return "#ddeabf"; default: return "#FFFFFF"; } }

But I get the following error when I try and load the page, I've put breakpoints in the converter and they are not hit. The process falls over before it gets into the converter.

Xamarin.Forms.Xaml.XamlParseException: Position 387:74. Cannot assign property "Converter": Property does not exists, or is not assignable, or mismatching type between value and property

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited December 2016

    Did you create the resource for your converter in the resource dictionary of the View/Page?

        <ContentPage.Resources>
            <ResourceDictionary x:Name="AppDictionary">
                <converters:StringNullOrEmptyBoolConverter x:Key="StringNullOrEmptyBoolConverter" />
                <converters:FileNameWithExtentionConverter x:Key="NameWithExtentionConverter" />
                <converters:ValueLessThanBoolConverter x:Key="ValueLessThanBoolConverter" />
                <converters:NullBoolConverter x:Key="NullBoolConverter" />
    

    Is your naming right? You have "backgroundColour" as the name of the x:Key of your converter. While a completely legal value, it doesn't follow normal naming conventions of including 'Converter' as part of the class name and key name.

    Instead of sending back a string and relying on implicit conversion, you might consider sending back an actual color object. return Color.FromHex("#008334");

    Unless you're using this converter in lots of places, you could also eliminate it completely by using DataTriggers in your XAML. Here the only converter is checking if a value is null or zero, but nothing as specific as hardcoding return values within the converter. So if there are unread notifications the background is red, if not its blue... and those colors are decided on a use-by-use case from the XAML not the converter - which means we can apply themeing values, or use the same logic for making icons visible/hidden etc. You don't have to use a converter as part of the check... You can also compare againt static values like true/false; I would think with a little experimentation you could compare against types. or have a converter that just confirms the type but doesn't force hardcoded return colors.

                <DataTrigger Binding="{Binding UnReadNotificationsCount,
                                               Converter={StaticResource NullOrZeroBoolConverter}}"
                             TargetType="Grid"
                             Value="False">
                    <Setter Property="BackgroundColor" Value="{StaticResource Blue4}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding UnReadNotificationsCount,
                                               Converter={StaticResource NullOrZeroBoolConverter}}"
                             TargetType="Grid"
                             Value="True">
                    <Setter Property="BackgroundColor" Value="{StaticResource RedBright}" />
                </DataTrigger>
    
  • NinjaFocksNinjaFocks USMember ✭✭

    @ClintStLaurent Yes I've included it in the resources. Managed to find a work around for this situation but I've been unable to use any converters at all since. My team are working on the same code base & are all able to create them it just seems to be me. Is there any known issues in newer version of Xamarin? The only difference between my setup and theirs is that I've updated Xamarin.

  • JohnHardmanJohnHardman GBUniversity mod

    @NinjaFocks - There is a known issue with background color in 2.3.3 - take a look at the thread at https://forums.xamarin.com/discussion/77854/xamarin-forms-2-3-3-stable/p4

  • NinjaFocksNinjaFocks USMember ✭✭

    @JohnHardman Thanks John I will have a look. Unfortunately I am have problems with others as will, today's issue was IsVisible on a button!

Sign In or Register to comment.