ItemsControl and Canvas positioning doesn't play nice together


When using an ItemsControl (wrapping our Canvas), each Widget added to the canvas gets wrapped in a ContentPresenter, which means there is now a layer in between the Widget and the Canvas in the VisualTree, and this object does not understand the Canvas positioning properties (Canvas.Left / Canvas.Top) that we set using databindings (or otherwise).
In other words, we cannot directly use a Canvas wrapped in an ItemsControl to position elements, and we need an ItemsControl to easily databind our elements to a canvas. Work-arounds include:
  • Wrap every Widget in its own Canvas, such that every Canvas is positioned at (0,0), but the Widgets get displaced. Not pretty, but possibly still the simplest. (Cf. http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1851496&SiteID=1)
  • Use Margin to position Widgets, instead of Canvas.Left / Top.
  • NEW: Use RenderTransform to position?
  • Implement own DependencyProperty for positioning our Widgets, delegating x/y values to the Canvas.Left / Top properties of the parent ContentPresenter, which itself is contained in our canvas.
Closed Dec 13, 2007 at 7:33 PM by thomas_darling