简体   繁体   English

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

[英]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 ,你有没有尝试过使用SelectedItemSelectedValue If the DataGrid 's ItemSource is set to your ObservableCollection , then those properties should point directly to the selected contained object.如果DataGridItemSource设置为您的ObservableCollection ,则这些属性应直接指向所选的包含对象。

暂无
暂无

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

相关问题 访问ObservableCollection中的项目绑定到WPF DataGrid - Accesing items in ObservableCollection bound to WPF DataGrid 为什么 DataGrid 项目即使绑定到在 WPF 中有行的 ObservableCollection 也是空的? - Why are DataGrid items empty even if it is bound to an ObservableCollection that has rows in WPF? WPF将项添加到绑定到observablecollection异常的datagrid - WPF add item to datagrid bound to observablecollection exception 从绑定到数据网格Silverlight的observablecollection中删除项目? - Removing items from a observablecollection bound to a datagrid Silverlight? 在MVVM C#中使用自定义变量将项目添加到绑定到ObservableCollection的WPF DataGrid中 - Adding items to a WPF DataGrid bound to an ObservableCollection with a custom mutator in MVVM C# DataGrid未绑定到ObservableCollection - DataGrid is not bound to the ObservableCollection 问:C#WPF数据网格绑定到ObservableCollection <T> 没有更新 - Q: C# WPF DataGrid bound to ObservableCollection<T> gets not updated WPF:在重新实例化绑定的ObservableCollection时,DataGrid上的水平ScrollViewer捕捉到右侧 - WPF: Horizontal ScrollViewer on DataGrid snapping to right side on reinstantation of the bound ObservableCollection 如何保存在wpf中绑定到ObservableCollection的tabcontrol项的选项卡顺序? - How to save the tab order of tabcontrol items which are bound to an ObservableCollection in wpf? WPF:具有ObservableCollection的绑定IsEnabled MenuItem <T> Items.Any() - WPF : Bound IsEnabled MenuItem with ObservableCollection<T> Items.Any()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM