c# - Binding a hierarchical data model to a DataGrid -
i have hierarchical data structure simple wpf application follows:
a tvshowcollection object contains tvshow objects. each tvshow object contains episodecollection object, in turn contains episode objects. model makes heavy use of inotifypropertychanged.
i need display list of of episode objects in grid, along name of tvshow.
if take of episodes structure , bind them itemssource of grid easy bind each property of episode column, how can bind 1 of columns related tvshow name property higher in hierarchy?
can flatten data somehow in viewmodel?
i hope makes sense.
thanks!
edit:
thanks maw74656, want end result - of episodes in system displayed, tvshow details repeated in grid (airdate property of episode):
image here: http://i.stack.imgur.com/sy1ib.png
i have working now, degree, not in way particularly like. i've put property on viewmodel grid return object containing information need:
public class tvshowgridviewmodel : baseviewmodel { private repository _repo; private tvshowcollection _alltvshows; public object alltvshows { { var flatlist = tvshow in _alltvshows 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 don't seem able make two-way binding. there better way?
try use objects instead of strings:
var flatlist = tvshow in _alltvshows 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"/>
Comments
Post a Comment