C# WPF Token is not valid on BrushConverter

c#, colordialog, colors, token, wpf

Solution

Your code will work if you change your method to this...

    private Brush DrawingColorToBrush(System.Drawing.Color color)
    {
        Brush ret = null;
        BrushConverter m = new BrushConverter();
        string s = "#" + color.ToArgb().ToString("X8");
        if (m.CanConvertFrom(typeof (string)))
        {
            ret = (Brush) m.ConvertFromString(s);
        }
        return ret;
    }

The key is to prepend the string with the '#' character.

Problem

I am converting a System.Media.Brush to a System.Drawing.Brush, but after I change the color. It throws a "Token is not valid" error on the converter. ``` private Brush DrawingColorToBrush(System.Drawing.Color color) { Brush ret; BrushConverter m; m = new BrushConverter(); ret = (Brush)m.ConvertFromString(color.ToArgb().ToString("X8")); return ret; } ``` The color is coming from a System.Windows.Forms.ColorDialog

Original source