none
WPF Programmatically set code behind Height or Width to “Auto” RRS feed

  • Question

  •  

    Hi,
     
    I have a window that has a hidden ListView. When clicking a button on the Window the ListView is populated and made visible if there is any data to show.  I would like the Window to size it's self to 'Auto' programmatically in the code behind if there is Data in the ListView.
     
    I've given my Window a name :
     
    <Window Name="myWindow" Height="300" Width="350" .....etc > ... </Window>
     
    When using intellisense in the code behind it says myWindow.Height is expecting a type of

     

    double FrameworkEliment.Height

     

    I can set it to a double value

    Eg:  myWindow.Height = 500;

     but can’t work out how to set it to “Auto”.

    Does anyone know how to set it to Auto?

     

    Thanks

     

    Matt.

    Thursday, February 7, 2008 10:48 AM

Answers

  • Try to use ActualWidth and ActualHeight properties to get values. To set values, use Width and Height properties.
    Thursday, February 7, 2008 2:19 PM
  • This is common for all FrameworkElement-derived classes, but for Window maybe there is an exception.
    Try using Window.SizeToContent property.
    Thursday, February 7, 2008 12:58 PM

All replies

  • Use this

    myWindow.Height =
    Double.NaN;
    • Proposed as answer by Valentin Dan Thursday, December 27, 2012 9:03 AM
    Thursday, February 7, 2008 12:33 PM
  • Sorry that didn't make any difference.

     

    Thanks

     

    Matt

     

    Thursday, February 7, 2008 12:43 PM
  • This is common for all FrameworkElement-derived classes, but for Window maybe there is an exception.
    Try using Window.SizeToContent property.
    Thursday, February 7, 2008 12:58 PM
  • Auto is the default, so if in doubt, use myWindow.ClearValue(WidthProperty) to clear any local value from the property.

     

    • Proposed as answer by _xr280xr_ Wednesday, November 24, 2010 8:36 PM
    Thursday, February 7, 2008 1:12 PM
  • Hi Tim,

     

    Using

     

    myWindow.ClearValue(HeightProperty);

    myWindow.ClearValue(WidthProperty);

     

    does indeed set each value to NaN however the screen size does not change to reflact that.  I'm looking for the screen to Auto size it's self.

     

    Thanks for your help though.

    Thursday, February 7, 2008 2:04 PM
  •  

    Hi EugeneZ,

     

    using ..

     

    Code Snippet

    myWindow.SizeToContent = SizeToContent.WidthAndHeight;

     

     

    .. did exactly what I wanted which is to size the content programaticaly.

     

    One thing I have noticed though is that using SizeToContent does not update the Width and Height properties of the window.

     

    I wanted to put in a restriction on the size of the auto size so it doesn't get larger than the screen display.

     

    Code Snippet

    if (myWindow.Height > 703)

    myWindow.Height = 703;

     

    if (myWindow.Width > 986)

    myWindow.Width = 986;

     

     

    but the Window Height and width is always the size you originaly set in the Xaml.

     

    Thanks

    Thursday, February 7, 2008 2:16 PM
  • Try to use ActualWidth and ActualHeight properties to get values. To set values, use Width and Height properties.
    Thursday, February 7, 2008 2:19 PM
  • Brilliant EugeneZ,

     

    You are a star, just what I want.

     

    Code Snippet

    if (myWindow.ActualHeight > 703)

    myWindow.Height = 703;

     

    if (myWindow.ActualWidth > 986)
         myWindow.Width = 986;

     

     


    Thanks

     

    Matt

    Thursday, February 7, 2008 2:30 PM
  • Try use this : 

    Height = new System.Windows.GridLength(1,System.Windows.GridUnitType.Auto);

    or:

    Height = new System.Windows.GridLength(1,System.Windows.GridUnitType.Star);

     

    Sunday, May 16, 2010 9:35 AM
  • None of this worked atleast for Infragistics XamRibbonWindow:(
    Saturday, August 7, 2010 3:22 PM
  • In my case as my object was a little bit different I used.

    dataGridTemplateColumn.Width = System.Windows.Controls.DataGridLength.Auto;

    I hope it helps


    Tuesday, February 16, 2016 7:37 PM