WPF Grid vs Stackpanel

2019-01-18 04:32发布

问题:

For WPF/Silverlight layout, is it better to use a Grid with lots of rows and columns, or tons of Stackpanels?

回答1:

You should use a Grid if you need things to line up horizontally and vertically. Use a StackPanel to create a row or column of things when those things don't need to line up with anything else.

However, don't limit yourself to those two options. In particular, have a look at the DockPanel. It's slightly more complex than a StackPanel, but its markup isn't as cluttered as the Grid. Here's a good article on the DockPanel:

Using the DockPanel in Silverlight 2



回答2:

The container you use should be based on the content and not whether one approach is better than another. If you need things to line up both horizontally and vertically, you really should use a grid. But other than that, it really depends on the content you intend to display.



回答3:

I think the Grid is a better idea. I usually set up the general layout with a Grid and use a few stackpanels here and there to do some specific stuff. I also have a feeling that performance is better with Grids and that Grids generally give you more flexibility.



回答4:

I don't think Grid is a better idea.

for example , if you want to insert a row to existing Grid layout document (in the middle)

there exising row is 1,2,3,4 , then the requirement is insert new row between 1 and 2.

then you had to change 2,3,4 to 3,4,5 (find all tag an change....)

think about if one row have 3 - 5 columns... it's a dirty job to reorder all digital.!!!



回答5:

I prefer the StackPanel because I find it easier to make changes when inserting new elements, rows or columns. With a grid you need to read row numbers and column numbers to find out where you are. With a StackPanel you just follow the nesting, this is easier and less messy than a grid.

For example, in a XAML page, I use a horizontal stack panel like a parent grid, then if I need a column, I have a separate "vertical" stackpanel nested. This way a horizontal stackpanel becomes a "grid" and the nested vertical StackPanel's become the columns. I find this easier to read and modify that rows and columns in a grid.



回答6:

I have no experience with such a layout, but I will bet, that the grid is easier to render compared to lots of nested stackpanels.