I'm trying to fill my list with system.drwaing.color items to pick a random color and set it to backColor.
Here is my code:
List<Color> myList = new List<Color>();
//rc.Add(Color.Chartreuse);
//rc.Add(Color.DeepSkyBlue);
//rc.Add(Color.MediumPurple);
foreach (Color clr in System.Drawing.Color)
{
//error
}
Random random = new Random();
Color color = myList[random.Next(myList.Count - 1)];
this.BackColor = color;
Error: 'System.Drawing.Color' is a 'type', which is not valid in the given context
Can anyone give me a Hand?
My scenario is the follow and i hope somebody to find it useful:
I have a ComboBox control in a wpf window that i want to display all the colors from the Brushes class.
MainWindow.xaml
In the window declaration i have add the follow reference:
In the Window.Resources section i have register the converter with the "ColorConverter" name:
And somewhere in my xaml code i have implement the follow combobox:
MainWindow.cs
StringToColorsConverter.cs
Some tips....
In the ComboBox.ItemTemplate binding you will find the binding to "Binding Path=." =>Since the colors list is not an object list but a list of strings,Binding Path=. is the way to bind the control to the string name
Also... Set te ComboBox HorizontalContentAlignment="Stretch" for streaching the Rectangle to the ComboBox width...try it to see the difference.
Keep coding, JJ
From this question:
List<Color> colorList = ColorStructToList();
Here is your code:
Based on Artxzta's answer, in VB.net: