wpfcanvaswpf-positioning

How to center an element in wpf canvas


How can I center an element in wpf canvas using attached properties?


Solution

  • Something like this.

    double left = (Canvas.ActualWidth - element.ActualWidth) / 2;
    Canvas.SetLeft(element, left);
    
    double top  = (Canvas.ActualHeight - element.ActualHeight) / 2;
    Canvas.SetTop(element, top);