dotnet / dotnet/wpf

Add implicit conversion between System.Drawing.Color and System.Windows.Media.Color

Open
#752 10 comments 14 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

Currently System.Windows.Media.Color is the color type used throughout WPF. This type however is not part of .NET Standard, which uses System.Drawing.Color instead.

It would be very useful that there's an implicit conversion between these two types, so that we can use either to bind in XAML or parse into for instance a color brush etc, so we can reuse these properties for various object models across multiple platforms.

Example:
```cs
public class MyViewModel
{
public System.Drawing.Color Color { get; set; }
}
```
```xml




```

This currently produces a binding error like this:
```
System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'System.Drawing.Color' and 'System.Windows.Media.Brush'. Consider using Converter property of Binding. BindingExpression:Path=Color; DataItem='MyViewModel' (HashCode=64844482); target element is 'Ellipse' (Name=''); target property is 'Fill' (type 'Brush')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='Color [Red]' BindingExpression:Path=Color; DataItem='MyViewModel' (HashCode=64844482); target element is 'Ellipse' (Name=''); target property is 'Fill' (type 'Brush')
```

I added something similar to Xamarin.Forms as well https://github.com/xamarin/Xamarin.Forms/pull/1359
(Also hoping we can add this to UWP too, so everyone plays nice with .NET Standard).

Implementation suggestion:
```cs
namespace System.Windows.Media
{
public struct Color
{
public static implicit operator System.Drawing.Color(Color color) =>
System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);

public static implicit operator Color(System.Drawing.Color color) =>
FromArgb(color.A, color.R, color.G, color.B);
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.