Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How to set the row height of a Grid in the code?
4 replies. Latest Post by darktatami on March 11, 2009.
(0)
darktatami
Member
19 points
16 Posts
03-11-2009 12:38 AM |
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.
bryant
Star
9447 points
1,558 Posts
03-11-2009 12:48 AM |
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.
03-11-2009 9:15 AM |
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?
03-11-2009 9:43 AM |
Just tell the image not to stretch and you won't need to set the width/height.
image.Stretch = Stretch.None;
03-11-2009 1:21 PM |
Awesome, thanks!