[英]Accessing items in ObservableCollection bound to WPF DataGrid
I have an ObservableCollection bound to a WPF DataGrid in which MyClass has a Boolean flag indicating if item has been seen or not.我有一个绑定到 WPF DataGrid 的 ObservableCollection,其中 MyClass 有一个布尔标志,指示项目是否已被看到。 If the item in the DataGrid is selected I want to set the MyClass.Seen property to true to indicate that it was viewed.
如果选择了 DataGrid 中的项目,我想将 MyClass.Seen 属性设置为 true 以指示它已被查看。 Initially, the SelectedIndex of the DataGrid matches to the index of the ObservableCollection but if I ever sort the DataGrid using any of the various columns that it displays, the SelectedIndex no longer matches with the ObservableCollection.
最初,DataGrid 的 SelectedIndex 与 ObservableCollection 的索引匹配,但是如果我曾经使用它显示的任何列对 DataGrid 进行排序,SelectedIndex 不再与 ObservableCollection 匹配。
Up until now I have been looping thru the ObservableCollection comparing a value in MyClass to a column value of the DataGrid's SelectedIndex row to find a match.到目前为止,我一直在循环遍历 ObservableCollection,将 MyClass 中的值与 DataGrid 的 SelectedIndex 行的列值进行比较以找到匹配项。 Is there a better way to accomplish this where referencing the SelectedIndex of the DataGrid points me to the correct item in the ObservableCollection?
在引用 DataGrid 的 SelectedIndex 将我指向 ObservableCollection 中的正确项目的情况下,是否有更好的方法来实现这一点?
Rather than using SelectedIndex
, have you tried using SelectedItem
or SelectedValue
?而不是使用
SelectedIndex
,你有没有尝试过使用SelectedItem
或SelectedValue
? If the DataGrid
's ItemSource
is set to your ObservableCollection
, then those properties should point directly to the selected contained object.如果
DataGrid
的ItemSource
设置为您的ObservableCollection
,则这些属性应直接指向所选的包含对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.