保留TreeView的复选框 [英] Persisting checkboxes of a TreeView

查看:100
本文介绍了保留TreeView的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有复选框的树视图。我正在寻找关于如何在树中保留复选框的一些指示或建议,所以当我重新启动我的应用程序时,我可以重新检查相同的复选框。我正在寻找关于如何做到这一点的建议? List会为此工作吗?什么是最好的结构/方案呢?



谢谢...

解决方案

你需要将UI与数据和应用程序的其他方面分开。创建UI中编辑的信息的一些数据模型。它可能只是一组纯数据类/结构。你应该知道这个模型,但模型不知道UI。



现在,开发,在最简单的情况下1)初始化模型的数据实例(一分钟,忘记持久性),2)从数据中获取UI的数量,3)使用此UI编辑的控件更新数据。



完成后,你几乎坚持不懈。对于持久性,请使用数据合同 http://msdn.microsoft.com/ en-us / library / ms733127.aspx [ ^ ]。



这种方法是最易于使用的非侵入式,强大的终端。您不需要修改类,只需添加一些属性即可。您不需要直接使用文件和XML或JSON。数据契约序列化器将根据契约简单地存储任何任意对象图(如果甚至不必是树,可以是任何东西,甚至是循环引用)并在以后恢复它,从/到任何文件流。性能也非常好,基于动态生成组件 System.Reflection.Emit



请看看我过去提出这种方法的答案:

如何在表单应用程序中使用XML文件编写器和阅读器? [ ^ ],

创建属性文件...... [ ^ ],

解析json字符串数组 [ ^ ]。



-SA

I have a treeview with checkboxes. I'm looking for some pointers or suggestions on how to persist the checkboxes in the tree so when I restart my app, I can recheck the same checkboxes. I'm looking for suggestions as to where I should be looking on how to do this? Will a List work for this? Whats the best structure/scheme to do this?

Thanks...

解决方案

You need to separate UI from data and other aspects of the application. Create some data model of the information edited in your UI. It could be just a set of some pure-data classes/structures. You UI should be aware of this model, but model unaware of the UI.

Now, develop, in the simplest case 1) initialization of the data instance of model (for a minute, forget persistence), 2) population of the UI from data, 3) update of the data from controls edited using this UI.

When this is done, you have all but persistence. For persistence, use Data Contract: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This approach is the most non-intrusive, robust end easy to use. You won't need to modify your classes, will need just to add some attributes. You won't need to work with files and XML or JSON directly. The data contract serializer will simply store any arbitrary object graph according to contract (if does not even have to be a tree, could be anything, even with circular references) and restore it later as it was, from/to any stream of a file. Performance is also very good, based on generation assemblies on the fly with System.Reflection.Emit.

Please see my past answers advocating this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA


这篇关于保留TreeView的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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