更新 DataGrid ByRef 中的 SelectedItem 并保留 SelectedItem 行为 WPF
我有一个数据网格,其中包含自定义对象的可观察集合。 selectedItem 绑定在我的视图模型中设置为 SelectedCustObject。我已经连接了一个属性 Changed 处理程序,这样我就可以在用户对 selectedItem 进行更改时随时更新数据库。当属性更新时,我的 Save() 子程序将触发并将自定义对象 ByRef 传递到我的中间层 (WCF) 并返回它。此时,数据网格将失去其 SelectedItem 状态。我不确定我的做法是否正确,或者是否有更好的方法。
SelectedItem="{Binding Path=SelectedMeterUsage, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
I have a datagrid with an Observable Collection of a custom object. The selectedItem binding is set to SelectedCustObject in my view model. I've wired up a property Changed handler so I can update the database anytime the user makes a change to the selectedItem. When the property is updated my Save() sub fires and passes the Custom Object ByRef to my middle layer (WCF) and returns it. The datagrid loses it's SelectedItem state at this point. I'm not sure if I'm going about this right or if there is a better way.
SelectedItem="{Binding Path=SelectedMeterUsage, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来好像引用
SelectedMeterUsage
正在更改,并且不再指向ObservableCollection
中的同一实例,因此网格不再知道选择了哪个项目。您需要更改更新代码以不更改引用,或者在保存后,您需要通过在ObservableCollection
中查找具有相同 ID 的项目并将其分配给 <代码>SelectedMeterUsage。It sounds as if the reference
SelectedMeterUsage
is changing, and no longer pointing to the same instance that is in yourObservableCollection
, therefore the grid no longer knows which item is selected. You will either need to change your update code to not change the reference, or after the save, you would need to reset the reference by finding the item in theObservableCollection
with the same ID and assigning it toSelectedMeterUsage
.