1
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{ 
    string userColourString = value.ToString();
    Debug.WriteLine(userColourString);
    long userColourNumeric = 0; 
    Int64.TryParse(userColourString, out userColourNumeric); 
    var colourToUse = userColourNumeric;
    return (Color)ColorConverter.ConvertFromString(string.Format("#{0:x6}", colourToUse));
}

I'm trying to convert the following two values into colours by using the converter method above but it's not working,. -2147483630 16777215

4

1 回答 1

1

十进制值16777215转换为FFFFFF十六进制。我测试了你的代码,的值colourToUse确实是"#ffffff". 这将很容易转换为白色。

十进制值-2147483630最终被转换为FFFFFFFF80000012十六进制。我不确定你希望它转换成什么颜色。难怪该ConvertFromString方法会引发格式异常。


您补充说您引用的是VB6 颜色常量的旧图表。

为了在该图表中生成颜色,您需要使用ColorTranslator.FromWin32

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var userColourString = value.ToString();
    int userColourNumeric = 0;
    int.TryParse(userColourString, out userColourNumeric);
    var colourToUse = userColourNumeric;
    return ColorTranslator.FromWin32(colourToUse);
}
于 2014-07-22T04:56:29.790 回答