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);
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With