0

I have some shapes on an WPF in VB.Net. I added a handler, so I'm able to delete or edit the shapes with my controls. For that I added an combobox where I wanna display all Windows.Media.Brushes so I can select one and add the color to the shape.

My Question: - How can I add all names of the Windows.Media.Brushes to a combobox? - How to coverte the names later back to a brush?

Best Regards, Stan

stan
  • 15
  • 4

1 Answers1

0

To get a list of all brush names just filter the properties of Brushes by their type

var brushes = typeof(Brushes).GetProperties()
                             .Where (pi => pi.PropertyType == typeof(SolidColorBrush))
                             .Select (pi => pi.Name)
                             .ToList();

to turn a name back into a brush you can use reflection again:

var brush = typeof(Brushes).GetProperty("Blue")
                           .GetValue(null) 
                as SolidColorBrush;
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Another way to convert string to SolidColorBrush is use `BrushConverter` (use `ConvertFrom` or `ConvertFromString` method). This is how the designer parses color name to brush in XAML. – King King Oct 09 '14 at 21:39
  • Cheers Stanley I could adopt your code to VB.NET to get the whole list of collers. Thank's King I am using your code to convert the colors later. The adopted Code looks like this: For Each brush In GetType(Brushes).GetProperties() comboBrushes.Items.Add(brush.Name) Next – stan Oct 11 '14 at 12:53