我想做的就是将列表框中的每个值与选定的值进行比较,然后将匹配索引设置为选定的。 由于某种原因,标题中出现了异常(exception)情况。我不明白为什么。 代码:
foreach(SurfaceListBoxItem n in BackgroundsList.Items)
{
if (n.ToString() == current) BackgroundsList.SelectedItem = n;
}
谢谢!
最佳答案
在WPF中,List.Items不一定包含ListBoxItem的集合,而是只包含数据值,并且数据的Item Container是派生的,要设置值,只需将current设置为所选项目即可。
无需迭代,只需执行以下操作即可,
BackgroundsList.SelectedItem = current;
关于c# - 无法将类型 'System.String' 的对象强制转换为 "..Controls.SurfaceListBoxItem' 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3308053/