Skip to main content

Microsoft Silverlight

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

(0)

darktatami
darktatami

Member

Member

19 points

16 Posts

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

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.


[ Darktatami ]
My Projects Blog

bryant
bryant

Star

Star

9937 points

1,629 Posts

Silverlight MVP
Answered Question

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

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

Member

19 points

16 Posts

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

 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

Star

9937 points

1,629 Posts

Silverlight MVP

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

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

Member

19 points

16 Posts

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

Awesome, thanks!

[ Darktatami ]
My Projects Blog
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities