views:

1317

answers:

2

Hey there. I hope this question hasn't come up before - I couldn't find it on the list of related questions.

Anyway, I'm a fairly newbie programmer (first year of a computing degree, so I know about squat), and I'm currently having a play with C# and WPF. For fun, I'm trying to create a simple music browser - to begin with, it'll just read my directory and list out the files, then launch one if you doubleclick or whatever. Anyway, I'm having trouble on how to store the TreeViewItems. I need to store it as a hierarchical design, so I can loop through or whatever and populate the Treeview once I've created all the paths, but I'm not sure of the best way to do this. Atm, I'm using TreeViewItem[][][] - an array of arrays that contain arrays, which actually seems wrong thinking about it...

thinking about it now, perhaps I should create a database, and populate the treeview from that? The only problem is, I've never done anything like that either XD.

Thanks for any help!

-Edibles

A: 

Let's take a step back and look at the overall architecture. In this scenario, you would typically have the TreeView display the data, a class type that represents the data, and the file/database/etc. as the storage for the data.

The pattern should be that you load the data from the file/database/etc. into the custom class type that represents the entity. Next, you would bind that source to the TreeView (either using WPF binding or manually binding).

Let's say you want each node in the tree to represent a music album. You would create a MusicAlbum class that is maybe composed of an Artist, album title, album date, SongList, etc. This data is serialized to and from a file/database/etc.

Next, you bind that data to the TreeView. WPF supports binding directly to lists of custom objects or you can manually configure the binding. If you are newer to development, you might want to consider manually binding since it is a little easier to configure, and it will give you good practice.

As far as the storage is concerned, I would recommend either a database (like SQLite) or an XML file. Since you are learning to develop, I recommend starting with an XML file. This is a good way of representing data and doesn't require the same overhead in complexity that a database does. If you were taking this to a production level, I would definitely recommend a database.

EDIT: The question itself is a little vague so you might want to be more specific as to what you are exactly asking for. Do you want code samples of TreeView binding or are you wanting guidance or overall structure?

j0rd4n
A: 

If your intention is to only access the music through an explorer style folder structure then you don't need a database since the windows filesystem already supplies you with a centralised point to get your data from. You do want to be careful about trying to populate the entire tree at startup though since you don't want the users waiting for the application to crawl the entire structure of their harddrive(s) before they can start navigating the tree. The process of only loading tree items when they are needed is called lazy loading, and is beyond a scope of an answer here. Fortunately this Code Project article shows how to do exactly this.

On the other hand, if you are looking to mimic the Windows Media player library style where the user supplies specific directories and you create a way to navigate the MP3s by artist, album etc. then you probably will want some kind of database in the background. In these cases you need to read the tag information from the mp3s in the selected folders, add the information into a nicely normalised database (to allow for fast searching and cross-referencing) and supply a UI that allows the user to navigate this way. Once again you'll be looking to lazy load the tree - select an artist then populate the child nodes with all their albums/songs from the database at the time of request - but there is a lot more work to complete before you get there (select a type of database, define the schema, learn how to parse the mp3 tags etc.) so I'll assume that you meant the first option.

Martin Harris