c# - 按项目名称选择项目 ID

标签 c# ienumerable

我得到了所有画笔颜色

var colorNames = typeof(Brushes)
                    .GetProperties(BindingFlags.Static | BindingFlags.Public)
                    .Select(x => x.Name);

如何获取颜色名称为“黑色”的颜色索引?

为什么我不能这样做 colorNames.FirstOrDefault(color => color.Name == "Black").Id;?

编辑:我对 DropDown 数据使用 colorNames,我需要特定颜色的索引以将其设置为默认 DropDown 值。

最佳答案

试试这个返回黑色的颜色索引,使用这个查询:

var colors = typeof(Brushes)
                .GetProperties(BindingFlags.Static | BindingFlags.Public)
                .ToList();

var index = colors.FindIndex(color => color.Name == "Black");

测试后,我得到了黑色索引的 8

关于c# - 按项目名称选择项目 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52127827/

相关文章:

c# - WPF,列表框中没有显示任何内容

C# Parallel.For 创建数组 : OK to put lock() on the array?

c# - 创建一个html文件并保存在一个文件夹中

c# - 使用 IPV6 的 UDP 客户端和服务器

C# Enumerable.Take 带默认值

c# - 如何使这个更通用

c# - IEnumerable<T> 作为返回类型

C# IEnumerable<string> 和 string[]

c# - 带有 X.509 证书连接错误的 Wcf 自托管服务

c# - 检查 IEnumerable<T> 是否有具有重复属性的项目