VB.NET: Output all Windows.Media.Brushes

592 Views Asked by At

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

1

There are 1 best solutions below

2
D Stanley On BEST ANSWER

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;