I kind of prefer to drag controls around on a canvas via the visual studio designer, so that each control has an x and y coordinate. I find it alot faster than using a grid. But I still see alot of articles out there that use a grid for laying out controls, so that each control has a row, column. What are your thoughts on this, is my approac开发者_如何学JAVAh wrong?
By using a canvas it's much harder to get everything resized in a good way, other controls will resize automaticly (if not fixed size ofcourse)
It takes some practice to use the various containers, but it is worth it. When your control is located in a container resizing works automatically and your XAML stays more organized.
Check out easy to use third part containers like autogrid.
Standard guidance from MS is to avoid using the Canvas for laying out controls.
Quote from MSDN Canvas documentation:
Canvas is the only panel element that has no inherent layout characteristics. A Canvas has default Height and Width properties of zero, unless it is the child of an element that automatically sizes its child elements. Child elements of a Canvas are never resized, they are just positioned at their designated coordinates. This provides flexibility for situations in which inherent sizing constraints or alignment are not needed or wanted. For cases in which you want child content to be automatically resized and aligned, it is usually best to use a Grid element.
So, I'd take that to mean that in most situations you would not want to use the Canvas and rather use the Grid (or another panel) to contain your various child UI elements. I personally find the Grid sufficient for most needs.
References:
Canvas - http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx
Globalization and Localization - http://msdn.microsoft.com/en-us/library/ms788718.aspx
To use grid (row & column ) is preferred because it does not depend on screen resolution.
Its wrong cause WPF supports other alternative read directions. If the localization changes, for WPF its impossible to remap X/Y coordinates, but in a Grids and StackPanels the logical arrangement can get inversed / changed.
Its very seldom good to absolutely position the elements
精彩评论