如何从WPF画笔中获得数组

本文关键字:数组 画笔 WPF | 更新日期: 2023-09-27 17:49:40

我想在WPF中创建现有Brushes的数组,以便我可以循环它们并在组合框中显示列表。我该怎么做呢?

我有这样的东西,但它不会工作,因为Brushes不是一个数组。

string[] brushes = Brushes;
foreach (string s in brushes)
{
    comboBox.Items.Add(s);
}

如何从WPF画笔中获得数组

您可以使用反射。您可以使用匿名类型来保存名称和笔刷。

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

只能通过以下方式访问:

var brushNames = values.Select(v => v.Name);