Question

When I drag a control from the toolbox on to the Visual Studio 2008 WPF design surface, it sets a margin based on where I dropped it and a default size.

Is there any way to prevent VS setting those? When I drop a control on a stackpanel I want it to flow in to the existing controls in the panel.

Thanks!

Was it helpful?

Solution

I've never found a way to prevent this. I just type the XAML instead.

EDIT:

I wrote the above nearly three years ago. In the meantime, it would appear that the design-time experience has gotten a lot better.

While the designer still adds hard-coded margins if you drag-and-drop, there are ways to remove those margins without having to do it by hand.

Here's a link to an article that illustrates how to use design tools in Visual Studio 2010 to lay out a form without having to type any XAML. (Despite the title, it's about WPF.)

The first part of the article shows hard-coded pixels, but I've linked to the second part that shows how to lay out the controls without that hard coding.

Layout Techniques for Windows Form Developers

OTHER TIPS

When I drop a control into a stackpanel, it doesn't add margins, it "flows" in with the rest.

The controls you drop onto the design surface have a margin by default because that's how it knows where it's placed. What I typically do is create a grid and set it's margin where I want it, then create columns and rows to put controls into. I drop in my other controls which will add margins which are usually not what I want. After I have all my controls into my grid, reasonably placed, I switch to XAML view and either remove the margin completely (which causes a control like a button to fill the entire space), or make the margin something like 5, which puts some nice padding around the control.

Bottom line...and this is especially true with Silverlight development, you must get used to manipulating the XAML directly. It's the only way you'll get the layout exactly where you want it. This is my opinion and most everybody in my shop who does WPF/silverlight development.

Unfortunately there does not appear to be a means to do this.

The default size is set as a result of you manipulating the size of your control is the designer. When you increase the width and/or height of a control the designer is interpreting your actions as a request to make the control that specified width and/or height.

A similar situation is encountered with the margins. As you alter the position of your control within the designer, it modifies properties to achieve the layout you've created. In the case of most containers the simplest solution is to modify margins in order to achieve the desired position, as you've already pointed out the simples solution is not always the most desired.

Personally I prefer to only use the XAML editor and only occasionally take a peak at the WPF Designer when I want to see the current layout. I also tend to favor using the Grid to layout most of my windows/controls as it provides the greatest flexibility not only for sizing and positioning, but for other tasks/features such as animations, transforms, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top