How can you ‘two-way bind’ a hierarchical data structure to a flat wpf datagrid?
The application interface model is iTunes – I am looking for the datagrid to appear flat (not a master/detail grid). The user sees rows containing an Artist, Album, and Song; but the data is stored hierarchically where an Artist contains a list of Albums which contains a list of Songs.
When the user renames an Album, the hierarchy automatically finds or creates the new Album (based on Album.Name) for the given Artist, and moves the Song to the Album.
Adding a translation layer between the flat data and the hierarchical data seems like a poor solution.
Update 1
After reading through the answers, it appears achieving what I’ve described above can’t be done without a lot of work behind the scenes. It seems I should reconsider representing my data in this fashion.
Flattening of the structure can be done by using a LINQ projection. See the one-to-many projection in this article.
All you have to take care of is that you re-query when the collection changes.
I do not see a way of making it any easier and a translation layer will always be needed whether it be a LINQ query or a special binding or a converter unless you find a grid that will do it for you.