使用 XML 和 Treeview 的分层数据
我正在制作一个用户界面,用户可以在其中添加和存储他们最喜欢的 YouTube 视频的链接。与浏览器中的收藏夹非常相似,但仅适用于 Youtube 视频。
我希望链接显示在树视图中,以便可以将它们编入类别和子文件夹等。有点像这样:
Videos
|---Music Videos
|---Music video 01
|---Music video 02
|---Music video 03
|---TV Shows
|---TV show 01
|---TV show 01
我目前最苦恼的是弄清楚用户如何存储这些数据用户界面关闭后。我一直在研究 xml 绑定,但我似乎无法创建一个看起来可以与树视图一起正常工作的像样的 xml 模式。
另外,我将如何存储 URL 和视频名称,但让树视图仅显示名称?
我将非常感谢任何有关解决此问题的最佳方法的指示。有更好的办法吗?这一切都非常令人困惑。
非常感谢。
I'm making a UI where the user can add and store links to their favourite Youtube videos. Very similar to a favourites folder in a browser, but just for Youtube videos.
I want the links displayed in a treeview, so that they can be catalogued into categories and sub-folders, etc. Sort of like this:
Videos
|---Music Videos
|---Music video 01
|---Music video 02
|---Music video 03
|---TV Shows
|---TV show 01
|---TV show 01
What I'm struggling with most at the moment, is working out how the user can store this data once the UI has closed. I've been looking into xml binding, but I can't seem to create a decent xml schema that seems to work properly with the treeview.
Also, how would I go about storing the URL along with the video name, but have the treeview display only the name?
I'd be very grateful for any pointers as to the best way to go about this. Is there a better way? It's all very confusing.
Thanks very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的数据架构是一个不平凡的问题,我只能提供我的一些想法,但不能保证它们都是好主意。
首先不建议直接使用 XML 数据,如果您尝试做更复杂的事情,它会变得非常不方便。
您可以将数据序列化为 XML,但您应该意识到它的局限性和副作用,在尝试比较各种序列化方法的 SO 上可以找到一些问题,您可能需要寻找其他格式的数据可能更合适。例如,XML 的两个方面非常明显:
无论如何,我都会使用数据库或普通对象模型并将其序列化/反序列化以进行存储。
如果您想允许子类别,您可能需要使用复合模式,然后您可以对象
Video
和Category
,均实现接口IVideoComposite
,其中Category
有一个属性例如,ObservableCollection
类型的Children
。然后,您可以使用隐式类型数据模板(
HierarchicalDataTemplate
for
Category
)来在TreeView
中创建项目。 (对于Category
之一,您将把ItemsSource
绑定到Children
)Good data architecture is a non-trivial issue, i can just offer some thoughts of mine but cannot guarantee that they are all a good idea.
Firstly would not recommend working directly with XML data, it gets very inconvenient if you try to do more complex things.
You can serialize your data to XML, but you should be aware of its limitations and side-effects, there are questions to be found here on SO which try to compare the various methods of serialization, you might want to look for those as other formats might be more suitable. Two aspects of XML are quite apparent for example:
In any case i would use a database or a normal object model and serialize/deseriale that for storage.
If you want to allow sub-categories you may want to use a composite pattern, you then can have an object
Video
andCategory
, both implementing the interfaceIVideoComposite
, whereCategory
has a propertyChildren
of typeObservableCollection<IVideoComposite>
for example.You then can use implicitly typed datatemplates (a
HierarchicalDataTemplate
forCategory
) to create the items in yourTreeView
. (For the one ofCategory
you will then bind theItemsSource
toChildren
)