CanOz Blog

Neil Knobbe's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

DecJanuary 2009Feb
SMTWTFS
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Archives

Image Galleries

vbCity Blogs

Once again it seems that a post from vbCity is to be fodder for a post here.

Although not part of the original query by vbcity member quicksun, the question of getting a Windows Presentation Foundation (WPF) Form to be resized as the expander control was either expanded or collapsed came up.

I went with what I thought was the quickest way about resizing the Form and that was to use the Collapse and Expand events of the expander control to reset the height of the WPF Form to its desired size.

Private Sub Expander1_Collapsed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Expander1.Collapsed
Win1.Height -= 40  ' Set the Height back to original size.
End Sub

Private
Sub Expander1_Expanded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Expander1.Expanded
Win1.Height += 40  ' Set the Height to new size.
End Sub

As simple an approach as the above code is there is an easier way to resize the Form thanks to the increased control programmers have been given over forms and controls with WPF.

With a WPF Form there is a property named SizeToContent.  By utilizing this property you can make the Form resize itself depending on, as the name implies, the content of the Form.

There are four setting for the SizeToContent property.

Manual
If you have SizeToContent set to Manual then the size of the WPF Form will not change from what is initially declared as the size of the Form either in the XAML or code behind of the Form.

Height
Setting the SizeToContent property to Height would mean that only the height of the Form will automatically change should the height of the Forms content become greater than the height of the Form.

Width
Similar to the Height setting, by setting the SizeToContent property to Width would mean that a WPF Form would automatically change widths should the Forms content become greater than the width of the Form.

WidthAndHeight
As you can probably guess by the name of the setting, WidthAndHeight would mean that the WPF Form can change size in both width and height should the Forms content be greater than the current size of the Form.

If you do use the SizeToContent setting you must remember that you can not specify a height, width or both depending on which setting you have.  If you want to give your WPF Form the ability to change in a direction then you must not declare a size for that property. 

For example if you want the WPF Form to be able to grow taller (height) then you would change the SizeToContent property to Height and not declare any value for the height of the WPF Form either in the XAML or property window.

All in all I think this is a nice little addition to what we can do with Forms. 

posted on Sunday, August 17, 2008 1:17 AM

Feedback

No comments posted yet.

Post Feedback

Title:
Name:
Url:
Comments: 
Protected by Clearscreen.SharpHIPEnter the code you see: