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;