简体   繁体   English

逐项选择项目名称

[英]Select item id by item name

I get all Brushes colors 我得到所有的画笔颜色

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

How can I get color index where color name is "Black"? 如何在颜色名称为“黑色”的地方获取颜色索引?

Why I can't go this way colorNames.FirstOrDefault(color => color.Name == "Black").Id; 为什么我不能这样做colorNames.FirstOrDefault(color => color.Name == "Black").Id; ?

Edit: I use colorNames for DropDown data, I need index of certain color to set it as default DropDown value. 编辑:我对DropDown数据使用colorNames ,我需要某种颜色的索引将其设置为默认的DropDown值。

Try this to return the index of the color that is Black, use this query: 尝试此操作返回黑色的索引,使用此查询:

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

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

After testing this, I get the value of 8 for the index of the color which is Black 测试完之后,我得到的值为8的黑色颜色索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM