C# WPF how to set location,width and height of the controls programmatically? C# WPF how to set location,width and height of the controls programmatically? wpf wpf

C# WPF how to set location,width and height of the controls programmatically?


To set Width and Height:

dockpanel1.width = 230;dockpanel1.height = 230;

as for location, wpf uses Margin:

dockpanel1.Margin = new Thickness(0,440,836,40);


It is possible to programmatically move child elements on a Canvas.

In xaml:

<Canvas>    <YourElement Canvas.Top="x" Canvas.Left="y"/></Canvas>

In C#:

Canvas.SetTop(YourElement, newX);Canvas.SetLeft(YourElement, newY);


Use some calculations like (control's previous position * layout's new size) / layout's previous size = control's new position

But the easiest way is to use XAMLUse Grid and put columns and rows in it and set the size of columns and rows to * So on layout size change, your controls will reposition in refer to parent's change in size which your grid is child of it.And you could even have auto resizable controls just by setting the controls' margins in columns and rows. Don't forget horizontal and vertical alignments set to stretch.