Skip to content

Two and One Way Converter Examples Don't Work #651

@Vossossity

Description

@Vossossity

Type of issue

Code doesn't work

Description

The given examples for creating a two way converter and one way converter have the wrong return types.
The code as written for the two way coverter is:

public class BoolToColorConverter : BaseConverter<bool, Color>
{
    public override Color DefaultConvertReturnValue { get; set; } = Colors.Orange;

    public override bool DefaultConvertBackReturnValue { get; set; } = false;

    public override string ConvertFrom(bool value, CultureInfo? culture)
    {
        return value ? Colors.Green : Colors.Red;
    }

    public override int ConvertBackTo(Color value, CultureInfo? culture)
    {
        return value == Colors.Green;
    }
}

it should be

public class BoolToColorConverter : BaseConverter<bool, Color>
{
    public override Color DefaultConvertReturnValue { get; set; } = Colors.Orange;

    public override bool DefaultConvertBackReturnValue { get; set; } = false;

    public override Color ConvertFrom(bool value, CultureInfo? culture)
    {
        return value ? Colors.Green : Colors.Red;
    }

    public override bool ConvertBackTo(Color value, CultureInfo? culture)
    {
        return value == Colors.Green;
    }
}

The one way converter has the same issue with the overide for ConvertFrom()

Page URL

https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/converters/

Content source URL

https://github.com/MicrosoftDocs/CommunityToolkit/blob/main/docs/maui/converters/index.md

Document Version Independent Id

c058518f-3a9b-7df9-6c1a-ec4bf98eefc6

Platform Id

9ba182ca-81a5-7dfc-3f78-99720fceb9da

Article author

@bijington

Metadata

  • ID: c058518f-3a9b-7df9-6c1a-ec4bf98eefc6
  • PlatformId: 9ba182ca-81a5-7dfc-3f78-99720fceb9da
  • Service: dotnet-communitytoolkit
  • Sub-service: dotnet-maui-community-toolkit

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions