簡體   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