繁体   English   中英

访问绑定到 WPF DataGrid 的 ObservableCollection 中的项目

[英]Accessing items in ObservableCollection bound to WPF DataGrid

我有一个绑定到 WPF DataGrid 的 ObservableCollection,其中 MyClass 有一个布尔标志,指示项目是否已被看到。 如果选择了 DataGrid 中的项目,我想将 MyClass.Seen 属性设置为 true 以指示它已被查看。 最初,DataGrid 的 SelectedIndex 与 ObservableCollection 的索引匹配,但是如果我曾经使用它显示的任何列对 DataGrid 进行排序,SelectedIndex 不再与 ObservableCollection 匹配。

到目前为止,我一直在循环遍历 ObservableCollection,将 MyClass 中的值与 DataGrid 的 SelectedIndex 行的列值进行比较以找到匹配项。 在引用 DataGrid 的 SelectedIndex 将我指向 ObservableCollection 中的正确项目的情况下,是否有更好的方法来实现这一点?

而不是使用SelectedIndex ,你有没有尝试过使用SelectedItemSelectedValue 如果DataGridItemSource设置为您的ObservableCollection ,则这些属性应直接指向所选的包含对象。

暂无
暂无

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

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