Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

How to set the row height of a Grid in the code? RSS

4 replies

Last post Mar 11, 2009 05:21 PM by darktatami

(0)
  • darktatami

    darktatami

    Member

    19 Points

    16 Posts

    How to set the row height of a Grid in the code?

    Mar 11, 2009 04:38 AM | LINK

    Hi, 

    I'm just getting started (frustrated with flash) and want to learn how to also do everything in code, not just xaml. I see a lot of resources on how to create stuff in the xaml.

    Currently I'm trying to set the row height of a Grid to [x]. When I call:

                var row = new RowDefinition();
                row.Height = new GridLength(150.0);

    It sets the scale of the row to 150%. How would I set the actual height of the row, but not affect what is inside.

    Also, is there a good resource (site, blog, book, etc) on how to do things in Code instead of xaml? I'm running into a lot of little things when trying to create dynamic elements/objects.


    Grid row height

    [ Darktatami ]
    My Projects Blog
  • bryant

    bryant

    Star

    10113 Points

    1662 Posts

    Re: How to set the row height of a Grid in the code?

    Mar 11, 2009 04:48 AM | LINK

    Your code shouldn't set it to 150%, it should only be 150 pixels high. What is in the row? Perhaps what you are putting in the row doesn't have a height so it is sizing to fit the area it has.

    -- bryant

    Blog | Twitter
    _________________
    Dont forget to click "Mark as Answer" on the post that helped you.
  • darktatami

    darktatami

    Member

    19 Points

    16 Posts

    Re: How to set the row height of a Grid in the code?

    Mar 11, 2009 01:15 PM | LINK

     That sounds like that's what is happeneing.

     

    I'm adding an image and not setting the dimensions, I guess what was throwing me off was that it was correctly drawing the image (which is a 300 x150). Even though it was drawing the image correctly 300x150 it didn't actually have the width and height set.

     

    Here is what I was doing:

               var grid = new Grid();
               grid.ColumnDefinitions.Add(new ColumnDefinition());

                var image = new Image();
                var imagePath = "../../Resources/Images" + path;
                var uri = new Uri(imagePath, UriKind.Relative);
                var bitmap = new System.Windows.Media.Imaging.BitmapImage(uri);
                image.Source = bitmap;
                image .SetValue(Grid.ColumnProperty, 0);
                image .SetValue(Grid.RowProperty,0);

                var row = new RowDefinition();
                row.Height = new GridLength(150.0);
                grid.RowDefinitions.Add(row);
                grid.Children.Add(newImage);

               _canvas.Children.Add(grid);
               grid.SetValue(Canvas.LeftProperty, ViewConstants.RESOLUTION_WIDTH - 300);
               grid.SetValue(Canvas.TopProperty, 25.0);
               grid.SetValue(Canvas.ZIndexProperty, 1);


    Setting the Height & Width in the code before I call the row.Height makes this work properly, thanks :)

    From what I can tell though, the code can not determine the width or height of an image that you add in this manner. How do you get those properties without hard coding them?

    [ Darktatami ]
    My Projects Blog
  • bryant

    bryant

    Star

    10113 Points

    1662 Posts

    Re: How to set the row height of a Grid in the code?

    Mar 11, 2009 01:43 PM | LINK

    Just tell the image not to stretch and you won't need to set the width/height.

    image.Stretch = Stretch.None;

     

    -- bryant

    Blog | Twitter
    _________________
    Dont forget to click "Mark as Answer" on the post that helped you.
  • darktatami

    darktatami

    Member

    19 Points

    16 Posts

    Re: How to set the row height of a Grid in the code?

    Mar 11, 2009 05:21 PM | LINK

    Awesome, thanks!

    [ Darktatami ]
    My Projects Blog