locked
Code Behind how to change a background color

    Question

  • Hello

    I searched answer on my question "How to set background color in code behind" but i found only examples where user use the color class or colors... I not have a color or colors class and i can't add System.Windows.Media in project becouse System.Windows have only "input" namespace...

    Monday, March 31, 2014 5:00 PM

Answers

  • That's a bit too simplistic, AmrKhamise. If you hardcode the colour directly you'll make things difficult for your designer and break the app in high contrast modes. It not horrid for toy apps, but it's not code you'd want to use on its own in a production app.

    Just as one of the first lessons you learn in CS is to never hardcode numeric values, you should never hardcode colours. That will make it difficult to update the colours later if you change your theme and can make the button unreadable in high contrast modes. Pulling the colours from resources (for Xaml apps) or CSS (for HTML apps) is strongly recommended, although if you need more customization you can check for high contrast, etc. in code.

    --Rob


    Tuesday, April 1, 2014 9:44 PM
    Owner

All replies

  • What language are you using so we can direct you to an appropriate forum?

    What specifically do you need help with? Are you writing a Windows Store app? What do you want to set the background color of and why? Depending on the specific scenario you'll probably want to draw the colours from resources so they can switch appropriately for high contrast modes.

    Setting a button's background is the example give in the FrameworkElement.Resources property documentation:

       void MainPage::SetBGByResource(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
        {
          Button^ b = safe_cast<Windows::UI::Xaml::Controls::Button^>(sender);
          b->Background = safe_cast<Windows::UI::Xaml::Media::Brush^>(this->Resources->Lookup("RainbowBrush"));
        }
    

    --Rob

    • Proposed as answer by Dave SmitsMVP Tuesday, April 1, 2014 9:53 AM
    Monday, March 31, 2014 6:59 PM
    Owner
  • the simple way 

    gd.Background = new SolidColorBrush(Windows.UI.Colors.Aqua);

    Tuesday, April 1, 2014 6:11 AM
  • That's a bit too simplistic, AmrKhamise. If you hardcode the colour directly you'll make things difficult for your designer and break the app in high contrast modes. It not horrid for toy apps, but it's not code you'd want to use on its own in a production app.

    Just as one of the first lessons you learn in CS is to never hardcode numeric values, you should never hardcode colours. That will make it difficult to update the colours later if you change your theme and can make the button unreadable in high contrast modes. Pulling the colours from resources (for Xaml apps) or CSS (for HTML apps) is strongly recommended, although if you need more customization you can check for high contrast, etc. in code.

    --Rob


    Tuesday, April 1, 2014 9:44 PM
    Owner