'Bind a MAUI control BackgroundColor to a property with data type System.Drawing.Color

I have a class with a Color property that is data type System.Drawing.Color, it is used for binding a Windows Forms control, and I want to bind a MAUI control BackgroundColor to it. The data type for the MAUI control background color is Microsoft.Maui.Graphics.Color. When I try binding the MAUI control straight to the Color, the control background color is just gray. That is understood because the data types are different. I tried adding a property to the class which returns the color as a string, my thinking on that was that the Binding takes a color name as a string, so it should be able to bind to a string property. It did not work either.

//class properties
public Color ShapeColor
        {
            get {
                return colorval;
            }
            set
            {
                colorval = value;
                InvokePropertyChanged();
                InvokePropertyChanged("ShapeColorToString");
            }
        }
    
public string ShapeColorToString { get { return this.ShapeColor.ToString(); } }

the following bound to Color did not work:

<Button x:Name="btn1"BackgroundColor="{Binding Path=ShapeColor}" />

nor did binding to color as string work:

<Button x:Name="btn2" BackgroundColor="{Binding Path=ShapeColorToString}"/>


Solution 1:[1]

The best solution I have so far is: Install Microsoft.Maui.Graphics into the class library (currently preview, so ensure that you include preview in the NuGet browse). And then add a property that converts System.Drawing.Color to Microsoft.Maui.Graphics.Color as so:

public Microsoft.Maui.Graphics.Color ShapeColorForMAUI { 
get => Microsoft.Maui.Graphics.Color.FromRgb(this.ShapeColor.R, this.ShapeColor.G, this.ShapeColor.B); 
}

I would rather not install MAUI graphics in the class library, but that is the best I can come up with.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Gellis