Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an array from WPF Brushes?

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);
}
like image 236
MCSharp Avatar asked Dec 04 '25 11:12

MCSharp


1 Answers

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);
like image 188
Mir Avatar answered Dec 06 '25 00:12

Mir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!