Binding a hierarchical data model to a DataGrid

I have a hierarchical data structure for a simple WPF application as follows:

A TvshowCollection object contains Tvshow objects. Each Tvshow object contains an EpisodeCollection object, which in turn contains Episode objects. The model makes heavy use of INotifyPropertyChanged.

I need to display a list of all of the Episode objects in a grid, along with the name of the Tvshow.

If I take all of the Episodes from the structure and bind them to the ItemsSource of the grid it is easy to bind each property of the Episode to a column, but how can I then bind one of the columns to the related Tvshow Name property which is higher up in the hierarchy?

Can I flatten the data somehow in a ViewModel?

I hope this makes sense.



Thanks MAW74656, this is what i want the end result to be - all of the Episodes in the system displayed, with the Tvshow details repeated in the grid (Airdate is a property of Episode):

Image here:

I have this working now, to a degree, but not in a way that I particularly like. I've put a property on the ViewModel for the Grid to return an object containing the information I need:

public class TvshowGridViewModel : BaseViewModel
    private Repository _repo;

    private TvshowCollection _allTvshows;

    public object AllTvshows
            var flatList = from tvshow in _allTvshows
                           from episode in tvshow.Episodes
                           select new { TvshowName = tvshow.Name, EpisodeName = episode.Name, EpisodeNumber = episode.Number, EpisodeAirdate = episode.Airdate };
            return flatList;

    public TvshowGridViewModel()
        _repo = new Repository("");
        _allTvshows = _repo.Tvshows;

But I don't seem to be able to make this a two-way binding. Is there a better way?


Try to use objects instead of strings:

var flatList = from tvshow in _allTvshows
               from episode in tvshow.Episodes
               select new { TvShow = tvshow, Episode = episode};

DataGrid should have explicitly specified columns:

<sdk:DataGridTextColumn Binding="{Binding TvShow.Name}" Header="Tv Show"/>
<sdk:DataGridTextColumn Binding="{Binding Episode.Name}" Header="Episode Name"/>

Need Your Help

call web parser from GUI python

python parsing user-interface pyqt beautifulsoup

I'm teaching myself object oriented programming in python. I'm using a basic GUI and a simple web parser - both of which are in separate class files. My goal is to call the web parser from the GUI....

Correct way to delete a NSLayoutConstraint

ios xcode swift autolayout nslayoutconstraint

In my apps I'm working a lot with constraints, in most cases with animations as well. In certain circumstances I need to remove constraints and add new ones.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.