![]() suggested so I needed another way, otherwise I would have made a VM property that returned a SolidColorBrush (or similar) from the RectangleColour string property. Unfortunately, WinRT hasn't got the that H.B. Public object ConvertBack(object value, Type targetType, object parameter, string language) Return new SolidColorBrush(ColorHelper.FromArgb(īyte.Parse(a, ),īyte.Parse(r, ),īyte.Parse(g, ),īyte.Parse(b, ))) Var b = hexString.Length = 8 ? hexString.Substring(6, 2) : hexString.Substring(4, 2) Var g = hexString.Length = 8 ? hexString.Substring(4, 2) : hexString.Substring(2, 2) Var r = hexString.Length = 8 ? hexString.Substring(2, 2) : hexString.Substring(0, 2) Var a = hexString.Length = 8 ? hexString.Substring(0, 2) : "255" If (hexString.Length != 6 || hexString.Length != 8) throw new FormatException() If (string.IsNullOrWhiteSpace(hexString)) throw new FormatException() Var hexString = (value as string).Replace("#", "") Public object Convert(object value, Type targetType, object parameter, string language) So I created a converter class: public class StringToSolidColorBrushConverter : IValueConverter Specifically, converting from a color to a SolidColorBrush. I made it with the intention that a 6-digit (RGB), or an 8-digit (ARGB) Hex value could be used either way. Class ColorConverter Provides a way to apply custom logic to a binding. You could always use the code from the converter class I created to re-use and do in your C# code-behind, or wherever you're using it, to be honest: Hopefully this will help people across the board, whichever situation they're in. I'm not sure whether you're using WPF or WinRT, but they do differ in some ways (some better than others). I posted this because in the last few months I have had to do this a bit, and kept forgetting how each time I wanted to, so hopefully this will serve as a useful reminder.Sorry to be so late to the party! I came across a similar issue, in WinRT. These methods provide two ways to convert a colour into a brush that you can then use to fill a rectangle or other UI element as necessary using programmatic means. Var brush = new SolidColorBrush(.Goldenrod) Īlternatively, if you have the colour stored in a variable then you can convert it like this: private void ConvertColour()Ĭolor col = .Goldenrod Converting from Ĭonverting from a system colour can be done by an instantiation of the SolidColorBrush type and feeding it the colour as a parameter in the constructor, like so: private void ConvertColour() For a full list of methods available to the brush converter, see the MSDN article here. ToString() method doesnt generate a Human readable String result like Green. You should use switch statement to convert it to some String value. There are other ways to convert colours to brushes using the brush converter, including from objects, mostly inherited from TypeConverter. Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert Dim S As SolidColorBrush value Return S. In the WPF, the hex code stands just as it were rgba. Var brush = (Brush)new ().ConvertFromString("#FF0000") Other variations may work but I’ve only tried and tested the one I’m about to show. It’s best if the string represents a colour code in a 6-digit hexadecimal format (“#FF0000” for example). Converting from StringĬonverting a colour to a string can be done using a brush converter. There can be various ways to do this depending on how you have your colour stored, so this post gives a couple of ways to convert a colour into a brush to style a UI element. Something that may be occasionally frustrating is trying to quickly colour a UI element on a WPF window programmatically when you know the colour code, or maybe even have a ‘color’ variable storing the colour for you.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |