Using Hexadecimal Color Values in WPF 3.5
28Mar08
I am currently preparing for my WPF content for the Philippine Launch Wave of Visual Studio 2008, Windows Server 2008, and SQL Server 2008. As a seasoned web developer, I am quite accustomed to the colors that I frequently use in hexadecimal and I intend to use these colors in my demo. I have a basic XAML file that has a Window with a ListBox, TextBlock and Slider in it. I am trying to show how LINQ works in a WPF 3.5 application.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="500" Width="500">
<Grid>
<ListBox Name="listBox1" SelectionChanged="listBox1_SelectionChanged" Margin="0,0,0,120" />
<TextBlock Height="105" Name="textBlock1" FontSize="{Binding ElementName=slider1, Path=Value}" VerticalAlignment="Bottom">Label</TextBlock>
<Slider Height="21" Margin="203,0,-5,0" SizeChanged="slider1_SizeChanged" ValueChanged="slider1_ValueChanged" Minimum="10" Maximum="50" Name="slider1" VerticalAlignment="Bottom" Value="10" />
</Grid>
</Window>While working on a certain functionality that I want to display, I became curious if I can use the colors in my sample application. The color, which I don’t exactly know the name but I refer to it as maroonish red (the UP blood in me), has an hexadecimal value of “#cd0001″. I tried appending that value in the Foreground property of the TextBlock (textBlock1 mentioned above) but it doesn’t work.
// This code won't work textBlock1.Foreground = "#cd0001"; // Even this textBlock1.Foreground = (Brush)"#cd0001";
To have a workaround on this, you need to use the BrushConverter class in able to use your hexadecimal based colors as a brush.
BrushConverter bc = new BrushConverter();
Brush brush;
brush = (Brush)bc.ConvertFrom("#cd0001");
textBlock1.Foreground = brush;
And it works! If you are more familiar with the colors that you want in hexadecimal, you can replace the hex value in the text above and most likely that would work.
Enjoy!
Filed under: Microsoft, Programming, Technology, WPF | 6 Comments







Awesome, was wondering how to do that, very useful!
Great! Glad to be of help!
You could use hexadecimal values directly
eg: Button
Reference:
http://books.google.lk/books?id=3UqXw90ju5IC&pg=PA184&lpg=PA184&dq=WPF+how+to+set+hexadecimal+color&source=bl&ots=SSlZ58-pj3&sig=1mdUaEE_KXoTIEj0DcaGssU1iXc&hl=en&ei=6X3DScT7EJGYsAPa35jzBg&sa=X&oi=book_result&resnum=8&ct=result
previous comment went badly with html formatting (of course I forgot this is wordpress
eg: <Button Background=”AARRGGBB”>Button</Button>
Here, AA – alpha, and RRGGBB – hexadecimal RGB values
http://books.google.lk/books?id=3UqXw90ju5IC&pg=PA184&lpg=PA184&dq=WPF+how+to+set+hexadecimal+color&source=bl&ots=SSlZ58-pj3&sig=1mdUaEE_KXoTIEj0DcaGssU1iXc&hl=en&ei=6X3DScT7EJGYsAPa35jzBg&sa=X&oi=book_result&resnum=8&ct=result
Interesting article.
If you want only the color, you can also use the ColorConverter class. (Altough this is not what you want here !)
+++
Brush myBrush;
myBrush = new SolidColorBrush ( Color.FromArgb ( 255, 255, 0, 0 ) );
This one works too.
Or the fastest way:
myTextBox.Foreground = new SolidColorBrush ( Color.FromArgb ( 255, 255, 0, 0 ) );
myTextBox.Background = new SolidColorBrush ( Color.FromArgb ( 255, 0, 255, 0 ) );
Happy Coding