.net – StackPanel vs DataGrid vs DockPanel in WPF

datagridnetwpfwpf-controls

I will need to dynamic generate a square matrix of "boxes"(e.g. 2×2, 3×3 etc.), each containing a textbox and a button. These boxes and text will also resize according the the size of the window. Shall I approach this problem using stackPanel, datagrid or dockpanel and what are their advantages or disadvantages? Appreciate for any suggestion.

Developed by WPF, VS2010.

Best Answer

I would approach it with a Grid control. Columns and rows inside the grid will auto size based on the size of the grid if you tell it too. Also, the Grid automatically fills the area they're put in to, which sounds like what you're after.

Stack panels aren't very flexible. The basically just put one item next to another with no auto sizing.

The dock panel is good for if you have content that should be fixed to on side and some other content for the centre.