Stack Panel Or Grid which one is most advisable
I heard that UI Design with Stack panel is more promotabl开发者_JAVA技巧e that Grids. Is there any fact present in that. I am usually using Grid for Designing my UI, recently in a document i read that the Grid consumes more time for rendering rather than stack panel ;; so i am very much confused about this.. Please give me more informations about this.. (WP7 Development)
In a Grid
, elements can be positioned relatively to each-other, being free to generally move around (if the developer wants to do so). A Grid
doesn't enforce specific positioning unless explicitly specified. In a StackPanel
the controls are obviously, stacked. So if in a Grid
you would add two controls in a single sequence, those will be overlapping. In a StackPanel
, the controls will be placed one after another, be it horizontally or vertically.
Generally, there is no performance overhead when it comes to choosing between Grid
or StackPanel
. A benefit of the Grid
control is that there is a possibility to explicitly define rows and colums, which ultimately leads to the possibility of creating more complex layouts. Each has its place and are generally not interchangeable.
Grid and Stackpanel each have their place it depends on your UI requirements. Sounds like you are prematurely optimizing.
I would implement the panel that most suits the need and then worry about performance later, if it is an issue.
Speaking from recent experience, a listbox is the best container class to use. The main benefit is speed as it uses a virtualizing stack panel internally.
I tried both a grid and a stack panel and both were taking 25 seconds to display 60 user controls. This dropped down to 10 seconds after I switched to the listbox.
If you needed the extra positioning, use a grid inside each list item template.
Just wanted to comment SteveChadbourne, but don't know how to do it.
See http://www.codeproject.com/KB/showcase/WP7-Performance.aspx (MonthCalendar) for an example of a grid with 126 elements that loads in roughly 2 secs. If you say that your 60 controls load in 25 secs, then it is not the question of the container used, but of your controls.
In such case, of course, UI virtualizing helps. But in general, there should be no important difference between the Grid and StackPanel. Grid should be a bit slower, but the difference won't be measurable.
There are more important things to optimize - templates, bindings etc. The above mentioned article describes the optimization steps that subsequently resulted in 5x faster MonthCalendar control. Programmers use to be surprised how inefficient are the advanced Silverlight tools.
Grid has the option to specify row or column index so that the elements can be ordered accordingly.So if you need more customization over your page elements , go with grid.Otherwise stack panel is enough.Grid has row definitions and column definitions so it has more objects than stack panel.Up to my testing , I didn't see any performance difference that can be visible to end user
Grid and Stackpanel are containers so they contain multiple children items. The difference is the way they contain elements.
Elements in Grid are stored in matrix form or you can say tabular form. You can define columndefinitions and rowdefinitions and you can access and store element at a particular row and column.
But in stackpanel, elements are stored in stack format .i.e. one after other. so it may be either vertical or horizontal.
so Based on your requirements, you can make most of these requirements.
精彩评论