Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make controls resize in WPF equivalently to Windows Form's anchor/dock properties?

Tags:

autoresize

wpf

I've read so many solutions to this problem. Every one of them fails to solve my problem. No matter what container I put the control into or what properties I set on the control/container it will not budge. I have a scroll viewer with a control within. I want it to resize with the window when the user resizes it at runtime. All I need is anchor=top, bottom, left, right. I don't understand why this is so elusive in WPF and why container objects and all kinds of property assignments need to be involved to accomplish what a single property can in Windows Forms. But every solution to this problem still results in my control staying at exactly its design time size as the window is resized at runtime. What's the simple way to get a grip on dynamic control sizing in WPF?

like image 311
Mike Avatar asked May 10 '11 20:05

Mike


People also ask

How do I resize a control in WPF?

In WPF, you can create a resizable shape by using the Path control. Simply set up your shape's path data and then set the Stretch property to Fill and your shape will resize itself to fit the space available. It even takes into account the stroke thickness.

How many UI containers are available with WPF?

User Interface Panels. There are six panel classes available in UI scenarios: Canvas, DockPanel, Grid, StackPanel, VirtualizingStackPanel, and WrapPanel.


1 Answers

This has caused me grief as well and AlexK helped me see the light. The trick is NOT to set the Height and Width.... Set these to AUTO and use the MARGIN to set the size. Set the HORIZONTALALIGNMENT and VERTICALALIGNMENT to STRETCH and then the anchor functionality works.

like image 174
www.redware.com Avatar answered Oct 07 '22 05:10

www.redware.com