WPF 网格与 Stackpanel
对于 WPF/Silverlight 布局,使用具有大量行和列的 Grid 或大量 Stackpanel 哪个更好?
For WPF/Silverlight layout, is it better to use a Grid with lots of rows and columns, or tons of Stackpanels?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您需要水平和垂直排列事物,则应该使用网格。 当这些东西不需要与其他东西对齐时,使用 StackPanel 创建一行或一列的东西。
但是,不要将自己局限于这两种选择。 特别是看一下 DockPanel。 它比 StackPanel 稍微复杂一些,但它的标记不像 Grid 那样混乱。 这是一篇关于 DockPanel 的好文章:
在 Silverlight 2 中使用 DockPanel
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
您使用的容器应该基于内容,而不是一种方法是否优于另一种方法。 如果你需要东西水平和垂直排列,你真的应该使用网格。 但除此之外,这实际上取决于您想要显示的内容。
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.
我认为网格是一个更好的主意。 我通常使用网格设置总体布局,并到处使用一些堆栈面板来执行一些特定的操作。 我还有一种感觉,网格的性能会更好,而且网格通常会为您提供更多的灵活性。
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.
我不认为网格是一个更好的主意。
例如,如果您想向现有的网格布局文档(中间)插入一行,
现有行是 1,2,3,4 ,那么要求是在 1 和 2 之间插入新行。
那么您必须更改 2 ,3,4 到 3,4,5(找到所有标签的变化...)
想想如果一行有 3 - 5 列...重新排序所有数字是一项肮脏的工作。!!!
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.!!!
我更喜欢 StackPanel,因为我发现在插入新元素、行或列时更容易进行更改。 使用网格时,您需要读取行号和列号才能找出您所在的位置。 使用 StackPanel,您只需遵循嵌套即可,这比网格更容易且不那么混乱。
例如,在 XAML 页面中,我使用水平堆栈面板(如父网格),然后如果我需要列,我会嵌套一个单独的“垂直”堆栈面板。 这样,水平堆栈面板就变成了“网格”,而嵌套的垂直堆栈面板就变成了列。 我发现这更容易阅读和修改网格中的行和列。
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.
两者都有优势(Grid/Stackpanel)。
网格的问题在于线路重组。
Stackpanel 的问题是没有表格结构(固定列宽)。
所以我认为这是解决这个问题的一个很好的解决方案:-)
定义样式
并在 Stackpanel 中使用样式
Both have strength (Grid/Stackpanel).
Problem with Grid is the lines restructuring.
The problem with Stackpanel is that no table structure (fixed columns width).
So I think that is a good solution about this problems :-)
Definition styles
And using styles into Stackpanel
我对这种布局没有经验,但我敢打赌,与许多嵌套的堆栈面板相比,网格更容易渲染。
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.