Using Hexadecimal Color Values in WPF 3.5

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"
    Title="Window1" Height="500" Width="500">
        <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" />

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.



31 thoughts on “Using Hexadecimal Color Values in WPF 3.5

  1. 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

  2. Sweet blog! I found it while browsing on Yahoo News.
    Do you have any tips on how to get
    listed in Yahoo News? I’ve been trying for a while but I never seem to get there!
    Many thanks

  3. you are really a good webmaster. The website loading speed is incredible.
    It kind of feels that you’re doing any distinctive trick. Furthermore, The contents are masterpiece. you have performed a excellent job in this subject!

  4. certainly like your website however you have to check the spelling on quite a few of your posts.

    Many of them are rife with spelling problems and I in finding it very troublesome to tell the
    reality nevertheless I will certainly come again again.

  5. I was curious if you ever considered changing the page layout of your website?
    Its very well worded; I love what you’ve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or 2 pictures. Maybe you could space it out better?

  6. You actually make it seem so easy along with your presentation however I find this topic
    to be really one thing which I feel I’d never understand. It kind of feels too complicated and very huge for me. I’m having a look
    forward for your next publication, I will try to get the
    dangle of it!

  7. My programmer is trying to persuade me to move to
    .net from PHP. I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using Movable-type on a number of websites for
    about a year and am nervous about switching to another platform.
    I have heard good things about Is there a way I can transfer all my wordpress posts into it?

    Any help would be greatly appreciated!

  8. When I originally left a comment I appear to have clicked on the -Notify me when new comments are
    added- checkbox and now each time a comment is added I get four emails
    with the same comment. There has to be a means you can remove me
    from that service? Thank you!

  9. After I originally left a comment I appear to have clicked the -Notify me when new comments
    are added- checkbox and now each time a comment is added I recieve 4
    emails with the exact same comment. There has to be a way you can remove
    me from that service? All the best!

  10. I’m not sure why but this site is loading extremely slow for me. Is anyone else having this problem or is it a problem on my end? I’ll
    check back later on and see if the problem still exists.

  11. Does your blog have a contact page? I’m having trouble locating it but, I’d like to shoot you an e-mail.

    I’ve got some ideas for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it improve over time.

  12. My buddy suggested I may like this weblog. He has been fully appropriate. The following publish truly produced our time. You can’t imagine the amount of time period I did expended due to this information! Appreciate it!

  13. Have you ever thought about including a little bit
    more than just your articles? I mean, what you say is important and everything.
    Nevertheless think about if you added some great visuals or video clips to
    give your posts more, “pop”! Your content is excellent but with pics and videos,
    this website could undeniably be one of
    the best in its niche. Great blog!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s