silverlight 4.0 - How to access a value based on siblings or parent in datagrid databinding -
sorry wordy title - battling work out if problems caused by
- bad class design (likely)
- ignorance (very likely)
- both (most likely)
i have spent ages googling due not understanding problem it's harder find solution.
basically, have "day" class has observablecollection of period called periods. each period has length of time. have method/property of day class (totaltimeupuntilthisperiod) takes in period , returns amount of time of periods until point.
i need put period level though datagrid can bind (it's itemssource set day.periods , it's datacontext set day). however, have calculation in day period instances don't know siblings , feels wrong if child walked tree parent level , summed children.
my thinking - period should have no "state" ie. no idea of in day in relation it's siblings - job of day class.
so, if class design correct - how can access datacontext in bound datacolumn? tried write converter take in period , parent datacontext (day) , call method return string bind @ period level) not figure out how make converter aware of datagrid datacontext , want know if on right track.
this bad design considered replicating parent property (e.g. totaltimeupuntilthisperiod) in child class (period) , somehow setting whenever duration changed bind got messy.
periods can added/removed datagrid/day can't calculated on rowload - has dynamic (i guess use linq sum lengths of previous periods)
so think questions are:
- is class design above ok? (ie. children should know nothing of parent or fellow siblings?)
- if so, how bind parent properties or methods in datagrid if value dependent on state of parent?
thanks pointers.
update:
here more things have tried in cold light of day:
1) text= "{binding datacontext.sometextfield, elementname=layoutroot}"
this not work - works fine when outside datagrid not when inside.
2) tried setting datacontext of textbox in templated datagrid column - works well, need pass in row datacontext , parent datacontext converter (in order lookup total time periods before one) , can't figure out how that.
so question may - how pass in the current row datacontext converter , parent datacontext (or "how make converter aware of parent datacontext").
here did in end - love know if practise or not:
i made parent class property on child class (this felt wrong?) - can reference parent's properties in datagrid.
Comments
Post a Comment