开发者

StackPanel vs DataGrid vs DockPanel in WPF

开发者 https://www.devze.com 2023-01-28 21:32 出处:网络
I will need to dynamic generate a square matrix of \"boxes\"(e.g. 2x2, 3x3 etc.), each containing a textbox and a button. These boxes and text will also resize according the the size of the window.Sha

I will need to dynamic generate a square matrix of "boxes"(e.g. 2x2, 3x3 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 stac开发者_如何学编程kPanel, datagrid or dockpanel and what are their advantages or disadvantages? Appreciate for any suggestion.

Developed by WPF, VS2010.


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.


Take a loot at the UniformGrid panel.


Stackpanels are useful when you have a certain amount of items that you want to be in line with each other. Very simple to use and effective when dynamically adding items, however it's simplicity means it doesn't really have any resizing abilities.

Dockpanels are useful when when placing and organising several different items onto a window, specifically when anchoring items to the top, bottom, left, right and then fitting to the remaining space in the centre (I recently discovered they're quite handy when used in conjunction with expanders). No real downsides, could well be effective for you.

If you have no need for dynamically changing the layout I would suggest that grid controls would be your best bet though as you can split the grid into the matrix you require. Using an asterisk (*) for each side would mean that all boxes would re-size evenly to fit the window you have. Functionality is limited but by the sounds of your requirements a grid would cover it.

A useful tutorial on layouts can be found here.

0

精彩评论

暂无评论...
验证码 换一张
取 消