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. 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() Now, change the Blend color picker for the window background from RGB to HLS (by right-clicking on one of the R-G-B-A letters in the color picker) then, drag the L slider to reduce the Luminance value to zero.
For a full list of methods available to the brush converter, see the MSDN article here. To see the difference, create a new WPF application in Expression Blend and set the window background to this solid color: FFA7BCD7. There are other ways to convert colours to brushes using the brush converter, including from objects, mostly inherited from TypeConverter.
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. To achieve the desired result, you need to create a converter with multiple conditions.
There are two ways to convert Brush to Color: (1) First convert Brush to string, and then to Color. How to change the color of a GridViewCell based on the value of a property of the underlying data item.
6m developers to have your questions answered on Background row color based on a column value of UI for ASP c program to change output text and background color in hindi How to use system function in c to execute dos Change console based application background and font color in c++ This would be easy on a standard checkbox by using a setter. new SolidColorBrush((Color)ColorConverter.ConvertFromString(3C3C3C)) 2. Brush brush new SolidColorBrush (color)) 4. Search: Wpf Change Background Color Based On Value. How to store and retrieve image using MVVM pattern in WPF C. BrushConverter brushConverter new BrushConverter () Brush brush (Brush)brushConverter.ConvertFromString ( string ) 3. How can I bind text and have it format itself on the UI WPF ListView change arrow icon. 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. Wpf: how to equally distribute the available height among 2 expander controls in grid without any gray area. 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.