I am trying to implement a MarkupExtension to convert rgba int value to System.Windows.Media.Color.
But I am getting an exception of
An object of the type "UIH:Color" cannot be applied to a property that expects the type "System.Windows.Media.Color"
Here is the implementation:
[MarkupExtensionReturnType(typeof(System.Windows.Media.Color))]
public class Color : MarkupExtension
{
public static explicit operator System.Windows.Media.Color(Color color)
{
return color.ToColor();
}
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public byte? A { get; set; }
public System.Windows.Media.Color ToColor()
{
if (A.HasValue)
{
return System.Windows.Media.Color.FromArgb(A.Value, R, G, B);
}
else
{
return System.Windows.Media.Color.FromRgb(R, G, B);
}
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return ToColor();
}
}
And I apply the color to a solidcolorbrush
<uih:Color x:Key="background" R="79" G="113" B="133" />
<SolidColorBrush x:Key="backgroundBrush" Color="{StaticResource background}" options:Freeze="True" />
The problem is that you are just using the wrong syntax. It is not a static resource what you need, but the markup extension syntax:
<SolidColorBrush x:Key="backgroundBrush" Color="{uih:Color R=79, G=113 ,B=133}" />
That should do the trick ;)
EDIT
If you want to reuse the Color then, as @Clemens pointed out, you do not need a markup extension or the new class at all, just declare a Color resource:
<Color x:Key="Color" R="79" G="113" B="133"/>
<SolidColorBrush x:Key="backgroundBrush" Color="{StaticResource Color}" />