The common question about positioning an element inside a Canvas
is "How to position the center of element (instead of the top left corner)".
- WPF: Resizing a circle, keeping the center point instead of TopLeft?
- WPF Center Ellipse at X, Y
- WPF element positioning on a Canvas
Several solutions are presented, but they all have drawbacks.
The easiest solution is to accommodate the element size during while setting the Canvas.Left
and Canvas.Top
properties programmatically. This works, but only once. This solution doesn't support bindings and it will break when the element size is changed. You also cannot set the Canvas.Left
or Canvas.Top
using
Another set of solutions involve translate transformations utilizing either RenderTransform
or Margin
. These solutions require binding some property to the -0.5 * Width
or -0.5 * Height
. Such binding requires creating a custom ValueConverter
and is impossible to create using only XAML.
So, is there a simple way to position an element inside canvas so that its Canvas.Left
and Canvas.Top
correspond to the element's center and both size and position properties can be bound to some other properties?