Reputation: 1068
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);
}
Upvotes: 3
Views: 3156
Reputation: 8469
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);
Upvotes: 13