Converting Hex Value to SolidColorBrush in Silverlight
I'm trying to write an IValueConverter
in Silverlight. This IValueConverter
will return a SolidColorBrush
. The converter will be passed a hex value like "FFFF5300". Because Silverlight does not have the BrushConverter
class, I needed to parse this value manually. In an attempt to do this, I have the following code:
byte a = (byte)(Convert.ToUInt32(color.Substring(0, 2), 16));
byte r = (byte)(Convert.ToUInt32(color.Substring(2, 2), 16));
byte g = (byte)(Convert.ToUInt32(color.Substring(4, 2), 16));
byte b = (byte)(Convert.ToUInt32(color.Substring(6, 2), 16));
My problem is, I can开发者_运维技巧't use the Convert.ToX
methods in an IValueConverter
. Because of this, I'm not sure how to convert a two character string into a byte value. Can someone tell me how to do this?
Actually, you can use the Convert.ToXXX()
methods in an IValueConverter
. You just need to add the System
namespace in front of Convert
: System.Convert.ToXXX()
.
I'm not sure if this what you're asking, but the following code doesn't use Convert
:
byte a = byte.Parse(color.Substring(0, 2), NumberStyles.HexNumber);
// etc.
The value must be in the following format "#xxxxxxxx",
System.Drawing.ColorTranslator.FromHtml(Value);
精彩评论