Question

I'd like to make an array of the existing Brushes in WPF so that I can loop them and show the list inside a combo box. How can I do this?

I have something like this but it won't work because Brushes isn't an array.

string[] brushes = Brushes;

foreach (string s in brushes)
{
    comboBox.Items.Add(s);
}
Was it helpful?

Solution

You can use Reflection. You can use an anonymous type to hold both the name and the brush.

var values = typeof (Brushes).GetProperties().
    Select(p => new { Name = p.Name, Brush = p.GetValue(null) as Brush }).
    ToArray();

You can access the names only through:

var brushNames = values.Select(v => v.Name);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top