在用 c# 编程一段时间后,我发现当我们在 comboBox 中有相同的项目时,我们无法获得正确的 selectedIndex。 假设我们有 ComboBox 和这些项目:
当我在 ComboBox 中选择第三项时,我想收到 2,但我总是收到 0。 当我在 ComboBox 中选择第五项时,我想收到 4,但我总是收到 3。
我认为 ComboBox
中的 SelectedIndex
总是返回 ComboBox 的第一个元素。
如何从具有相同项目的组合框中获取选定项目索引?
最佳答案
我怀疑你正在绑定(bind)到列表字符串。
字符串是引用类型,但它会覆盖 = 并找到第一个值匹配。
创建一个只有一个字符串属性的简单类。
public class SimpleString
{
public string StrValue { get; set; }
public SimpleString() { }
public SimpleString(string strValue) { StrValue = StrValue; }
}
关于c# - comboBox 中的 selectedIndex 在某些情况下不返回正确的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15343308/