应该如何数据的WinForms GUI控件和客户端类之间同步? [英] How should data be synchronized between a WinForms GUI control and the client class?

查看:479
本文介绍了应该如何数据的WinForms GUI控件和客户端类之间同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用什么方法被认为是标准保持与由应用程序维护的数据结构同步GUI控件中的数据结构?

What method is considered the "standard" for keeping data structures within GUI controls synchronized with the data structures that are maintained by the application?

例如:
    在的WinForms,如果创建一个ListView的实例,而不是将其指向一个数据结构,重新presents的项目,在列表中出现,必须编程实例ListViewItem的(S)和调用。新增方法来手动复制它们,一个接一个,进入一个数据结构,它是内部到ListView本身。这是有意义的,从一个线程的观点来看,它也使得渲染的控制应当要求一个专门的数据结构中存在的量单独控制知道关于维护的信息的上下文中感

For example: In WinForms, if one creates a ListView instance, rather than pointing it to a data structure that represents the items to appear within the list, one must programmatically instantiate ListViewItem(s) and call an .Add method to manually replicate them, one by one, into a data structure that is internal to the ListView itself. This makes sense from a threading standpoint, and it also makes sense within the context of rendering that a control should require a specialized data structure to exist for which the control alone knows the details regarding maintenance.

然而,这产生两个问题:

However, this creates two issues:

冗余:
 如果客户机类管理其自己的实体的列表,以允许用户从WinForms的用户界面当中选择,这整个列表必须被读出,转换,然后通过方法如重新UI控制的内部:。新增(ListViewItem的项)列出了目前占据两倍的内存。

Redundancy: If the client class manages its own list of entities, to allow the user to select among them from the WinForms UI, this entire list must be read, converted and then recreated inside of the UI control via methods such as: .Add(ListViewItem item) Lists now occupy twice as much memory.

的复杂性:
 由于两个列表现在存在,必须通过编程确保它们保持同步。这与从客户端类的集合对象解雇,或者程序员可以简单地小心随时添加事件来实现,当他们添加/其他删除/从一个列表中删除。

Complexity: Since two lists now exist, one must programmatically ensure that they remain synchronized. This can be achieved with events that are fired from the client class's collection object, or a programmer can simply be careful to always add/remove from one list when they add/remove from the other.

我见过许多情况下,程序员将使用一个UI元素就像一个ListView是用于维护该列表的实际集合对象的快捷方式。例如,每个用户输入的项目将被立即插入到ListView中,然后,当谈到时间通过在ListView访问用户的表项,该申请只是迭代。这种方法失败,当你正确地从UI逻辑业务分隔条件/应用逻辑适用。

I have seen many instances where programmers will take the shortcut of using a UI element like a ListView as the actual collection object used to maintain the list. For example, each user entered item will be immediately inserted into the ListView, and then when it comes time to access to user's entires, the application just iterates through the ListView. This method fails to apply when you are properly seperating business/application logic from UI logic.

总体而言,有些事情看起来不正确有关存储一个数据结构,它是内部的GUI控制中的应用数据。同样,存储两份名单,并保持它们programmitically同步似乎并不像一个优雅的解决方案无论是。理想情况下,将需要仅向具有到驻留在客户端的范围内的一个列表的引用提供UI元素

Overall, something just doesn't seem right about storing application data within a data structure that is internal to a GUI control. Likewise, storing two lists and keeping them programmitically synchronized doesn't seem like an elegant solution either. Ideally, one would need only to supply a UI element with a reference to a list that resides within the scope of the client.

那么,什么是正确的方式来处理这个问题呢?

So, what is the "right" way to approach this problem?

推荐答案

每一个UI控件需要其自身的一些状态。对于复杂的控制(如ListView控件)的状态也相应复杂。诀窍是使控制状态的维持尽可能简单。使用标准的ListView,这是不可能的 - 程序员必须做的工作。

Every UI control needs some state of its own. With complex controls (like ListView) that state is correspondingly complex. The trick is to make the maintenance of the controls state as simple as possible. With a standard ListView, that's not possible -- the programmer has to do the work.

这是一个原因,我写了 ObjectListView (一个开源的包装围绕.NET的WinForms ListView控件)。它可以让你在一个较高的水平,其中控制状态的维持是无形使用一个ListView。一个ObjectListView在模型上直接进行操作对象:

That's one reason I wrote ObjectListView (an open source wrapper around .NET WinForms ListView). It allows you to use a ListView at a higher level where the maintenance of the controls state is invisible. An ObjectListView operates on your model objects directly:

this.objectListView1.Objects = listOfModelObjects;
this.objectListView1.SelectedObject = aPerson;

一旦你可以在这个级别上工作,数据绑定本身是没有那么有用。但是,如果你真的想使用它,你可以使用从的 ObjectListView 项目。它为您提供了两全其美。

Once you can work at this level, data binding itself is no so useful. But, if you really want to use it, you can use the data-bindable DataListView from the ObjectListView project. It gives you the best of both worlds.

使用一个ObjectListView,没有理由切换到远不如有趣的DataGridView。一个ObjectListView为您提供了方便的DataGridView与一个ListView,那么更多一些漂亮的UI功能,再加上:

With an ObjectListView, there is no reason to switch to the far less interesting DataGridView. An ObjectListView gives you the ease of DataGridView with the nice UI features of a ListView, then plus some more:

这篇关于应该如何数据的WinForms GUI控件和客户端类之间同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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