MCSharp
MCSharp

Reputation: 1068

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);
}

Upvotes: 3

Views: 3156

Answers (1)

e_ne
e_ne

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

Related Questions