WPF background settings color (background color, foreground color)

Source: Internet
Author: User

Sometimes you also encounter situations where you want to set a background color for a control in the WPF background or to set the foreground color for the text.

I recently saw a custom ComboBox to read the system font and color of the instance, which implemented with the font ComboBox options to change the font settings, and color ComboBox is not implemented, so I also want to set up. The results found that the color from the foreground seems to be bad set to dynamic, so try to combobox_selectionchanged event settings from the background.

To set the color, first get the color. The way to get color I summed up five kinds:

 1, color color = COLOR.FROMARGB (transparency, red number, green number, blue number);//These four numeric ranges are (0-255) color color = Colo

R.fromrgb (red number, green number, blue number);/default transparency is 255; 2, color color = (color) colorconverter.convertfromstring ("Blue"); 3, if (E.source is ComboBox) {var value = (E.source as ComboBox).                   
                SelectedValue; int start = value. ToString ().
                IndexOf ("Color"); String Selectcolor = value. ToString ().
                Substring (start+5);    

       Color color = (color) colorconverter.convertfromstring (Selectcolor); TextBlock.  

       foreground = new SolidColorBrush (color); } 4, Tblock_content. foreground = Brushes.blue; (For the time when there are brushes) 


Here we highlight the third, value gets the values of: System.Windows.Media.Color color name (such as System.Windows.Media.Color Blue). So we need to intercept the name behind, and then use (color) colorconverter.convertfromstring (Selectcolor) to convert to color format. Note that you cannot use Colorconverter.convertfromstring (selectcolor) as color; hint that the error Color must be a non-null value.

5  SolidColorBrush MyBrush = new SolidColorBrush (System.Windows.Media.Color.FromArgb (0xFF, 0x25, 0x56, 0x87));

 button. Background = (System.Windows.Media.Brush) MyBrush;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.