为什么绑定到结构不起作用? [英] Why Does Binding to a Struct Not Work?

查看:28
本文介绍了为什么绑定到结构不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个问题,我将 ObservableCollection 绑定到 ListView.人是我写的一个结构.只要我在绑定之前设置 People 对象的值,一切似乎都可以正常工作.但是,当我尝试在运行时从 GUI 设置值时,底层对象似乎没有反映更改.

I've recently encountered an issue where I have an ObservableCollection bound to a ListView. People is a structure which I have written. So long as I set the value(s) of the People objects prior to binding, everything seems to work OK. However, when I try to set the values from the GUI at runtime, the underlying objects do not seem to reflect the change.

我最终通过简单地将 People 从结构更改为类来克服这个问题.无需其他更改.

I finally overcame this problem by simply changing People from a structure to a class. No other changes were necessary.

有人可以向我解释这是为什么吗?

Can someone please explain to me why this is?

推荐答案

您的绑定获得了 struct 的副本,因为结构是按值传递给方法的.如果绑定更新了一些东西;内存中某处的副本正在被修改,因此您的原始对象没有更新.

Your binding gets a copy of struct since structs are passed by value to methods. If the binding updates something; a copy in memory somewhere is being modified and hence the original object of yours is not updated.

这篇关于为什么绑定到结构不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆