Click here to Skip to main content
Click here to Skip to main content

Tagged as

Setting non-numeric grid row/column sizes in WPF/Silverlight

, 22 May 2010
Rate this:
Please Sign up or sign in to vote.
Programatically set grid row/column sizes (yes, even "Auto", "*", and "X*" - thanks Nish).
I've started working on a Silverlight application at work, and I had the need to create a Grid control programatically. If you've done any of this yourself, you know that you can set a row height or column width to either "Auto", "*", or a numeric pixel value (in your control's XAML). It took me a bit of time to find the answer on the web, and I ended up with the following method.
 
//--------------------------------------------------------------------------------
/// <summary>
/// Creates and returns a row definition object.
/// </summary>
/// <param name="height">The desired height attribute</param>
/// <returns>A RowDefinition object</returns>
private RowDefinition MakeRowDefinition(string height)
{
    RowDefinition rowDef = new RowDefinition();
    switch (height.ToLower())
    {
        case "auto" : rowDef.Height = new GridLength(1, GridUnitType.Auto); break;
        case "*"    : rowDef.Height = new GridLength(1, GridUnitType.Star); break;
        default     :
            {
                double pixels;
                if (!Double.TryParse(height, out pixels))
                {
                    throw new Exception("Invalid row definition pixel value.");
                }
                rowDef.Height = new GridLength(pixels, GridUnitType.Pixel);
            }
            break;
    }
    return rowDef;
}
 
The secret is the GridLength object, and after searching on google for the answer, I realized that Intellisense would have provided the answer I needed. You can extend this method to include other grid properties, but my only requirement was for row height, so I stopped there.
 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 22 May 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid