我的 Winforms 编码方法是否老派
我正在从事的项目相当大,我们开发了一个框架,可以轻松构建简单的 UI 屏幕。我们拥有的两种基本类型是搜索(搜索参数+结果网格)和详细信息(通常由某些模型对象填充的一组编辑器)。
该应用程序全部是 C# .NET Winform。
在详细信息中,我们有以下过程。
- 加载 - 根据适当的模型对象填充编辑控件。在显示详细信息之前调用
用户单击“确定”
- 验证 - 验证详细信息以确保一切一致
- 接受 - 将更新的控制值复制回模型
这对于简单的东西来说都很好用,但在更复杂的情况下,我注意到也许上述方法并不是最顺利的。
上面提到的更复杂的情况是,详细信息表示模型对象,并且详细信息中嵌入了一个网格,其中包含可以添加和删除的“子”对象。通常,您希望启动子详细信息并传入父模型对象,但此时它尚未完全填充/最新,因为只有单击“确定”时才会发生这种情况。我发现自己有时会以一种烦人的方式解决这个问题,这导致我提出以下问题。
从高层次来看,像我描述的那样,详细信息屏幕的可接受/最佳实践方法是在控件更改时将值复制到模型对象,而不是等到单击“确定”吗?
如果是这样,在 Winforms 应用程序中,实现此目标的最佳方法是什么?我发现一些文章提到了 Control.DataBindings,但它并不理想,因为绑定缺乏编译时安全性。我读过 WPF 具有良好的绑定支持,但不幸的是,我没有使用 WPF。
The project I'm working on is quite large and we have a framework we developed for building simple UI screens easily. The two fundamental types we have are Search (search parameters + grid of results) and Detail (a set of editors that are usually populated from some model object).
The application is all C# .NET Winforms.
In the Detail we have the following process.
- Load - Populate the edit controls based on the appropriate model object. Invoked just prior to the Detail being shown
User clicks ok
- Validate - Validates the detail to ensure everything is consistent
- Accept - Copy the updated control values back into the model
This all works nicely for simple stuff but in more complex cases I've noticed perhaps the above approach is not the smoothest.
The more complex cases mentioned above are when a Detail represents a model object and there is a grid embedded in the Detail which holds 'child' objects which can be added and removed. Typically you want to launch the child Detail and pass in the parent model object, however it is not fully populated/up to date at this point because that only happens when OK is clicked. I find myself working round this in an annoying fashion sometimes which leads me to the following question.
At a high-level, is the accepted/best practice approach for Detail screens like I describe to copy values to the model object when the control is changed, rather than waiting until OK is clicked?
If so, in a Winforms app, what is the best way to achieve this? I found some articles mentioning Control.DataBindings
but it's not ideal because of the lack of compile-time safety on the binding. I've read WPF has good binding support, but unfortunately, I'm not using WPF.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 Winforms,我建议您研究模型-视图-演示者模式。
http://msdn.microsoft.com/en-us/magazine/cc188690.aspx
For Winforms I would suggest that you look into the Model-View-Presenter pattern.
http://msdn.microsoft.com/en-us/magazine/cc188690.aspx
这可能会有所帮助:
演练:使用两个 Windows 窗体 DataGridView 控件创建主/详细信息表单
http ://msdn.microsoft.com/en-us/library/y8c0cxey.aspx
This might help:
Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controls
http://msdn.microsoft.com/en-us/library/y8c0cxey.aspx