c# - WPF What happens when a child's width is bound to a parent's actualwidth -


so if have binding placed on width of child object links parent's actualwidth, happen?

my guess parent measures how width child wants, child tells parent 0 width, parent given actual space during arrange, , attempts give child 0 since child didn't want any. actual width of space given parent should cause binding change child's width. @ point guess layout performed again.

however, assumes binding doesn't propagate fast. i'm still hazy on when bound value propagates target. depends on when parent's actualwidth value changes. occur after layout finishes? , bound pieces update? every binding interrupt running code go update target value? if not, wouldn't cause problems if 1 binding propagates change requires redraw, binding propagates different change causes redraw, etc.

some people asked actual problem was:

so wanted have control stretch fill available space. simple enough, wanted have in scrollviewer. scrollviewer gives infinite space children during measure. instead can bind width , height of child control parent's actualwidth , actual height; layout second pass, , seems swell.

however, later having issues of similar kind stretching control in controltemplate found out can set minwidth , alignment=stretch have stretch.

however, distinctly remembered trying earlier on other control , having not work, went , tried figure out difference between 2 cases was. boiled down 1 of them being in stackpanel few levels up.

so using binding 1 , minwidth plus alignment method other. anyway interested in make sure way i'm doing things doesn't create weird bugs later.

i hoping layout doesn't run when width or height changed instead system rechecks size changes periodically

according dispatherpriority enum, databinding occurs before rendering.

  • send
  • normal - constructors run here
  • databind
  • render
  • loaded
  • background
  • contextidle
  • applicationidle
  • systemidle
  • inactive
  • invalid
  • input

so binding try evaluate first before render occurs.

however rendering can cause binding update, if process of rendering parent panel increase width of panel (for example, parent panel placed inside of panel automatically stretches children take 100% of space grid or last element in dockpanel), trigger update on binding , increase width of child during render cycle.

the 2nd part of this answer may understand. take note of #6 too.

sequence of events when window created , shown

as requested, here sequence of major events in wpf when window created , shown:

  1. constructors , getters/setters called objects created, including propertychangedcallback, validationcallback, etc on objects being updated , objects inherit them

  2. as each element gets added visual or logical tree intialized event fired, causes styles , triggers found applied in addition element-specific initialization may define [note: initialized event not fired leaves in logical tree if there no presentationsource (eg window) @ root]

  3. the window , non-collapsed visuals on measured, causes applytemplate @ each control, causes additional object tree construction including more constructors , getters/setters

  4. the window , non-collapsed visuals on arranged

  5. the window , descendants (both logical , visual) receive loaded event

  6. any data bindings failed when first set retried

  7. the window , descendants given opportunity render content visually

steps 1-2 done when window created, whether or not shown. other steps don't happen until window shown, can happen earlier if triggered manually.


Comments