与 GUI 控件的对象同步
每次当我更改表单控件中的某些值时,我都需要设置对象的属性,反之亦然。我不想为每个控件编写一些方法,而是想要一个针对 GUI 元素的通用解决方案。我不使用 WPF,只使用 C# 3.0。
有什么建议吗?
谢谢。
Every time when I change some values in form controls, I need to set a property of an object or vice versa. Instead of writing some methods for each control, I want a general solution for GUI elements. I do not use WPF but only C# 3.0.
Any suggestions?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有控件都会公开表明其值已更改的事件。您可以订阅它并更改另一个控件的值。
All controls expose events that signal that their value changed. You can subscribe to it and change another control's value.
您可以将该对象设置为表单中的一个字段。当从控件触发相关事件时,然后对对象调用适当的操作。
或者,拥有一个将表单存储为字段的 Presenter/Controller 对象。它可以将其作为构造函数中的参数。然后,该演示者可以订阅您表单的相关事件并采取适当的行动。您可以更进一步,从表单和程序中提取一个接口到演示器中,这将有助于测试。看看 MVP 模式。
You could make the object a field in your form. When the relevant event fires from a control, then call the appropriate operation on the object.
Alternatively, have a Presenter/Controller object that stores your form as a field. It could take it as a parameter in it's constructor. This presenter can then subscribe to your form's relevant events and act appropriately. You could go further with this and extract an interface from your form and program to that in the presenter instead which would help testing. Have a look at the MVP pattern.