In WPF, add child to specific grid column and row using c# and not xaml

M

moondaddy

I have WPF window and a grid. The grid has several columns and rows. How
do I add an object to a specific row and column of the gird using c#?

If I use:


myGrid.Children.Add(dg);

then dg will be added to

Grid.Column="0" Grid.Row="0"

By default. I want to add it to

Grid.Column="1" Grid.Row="1"

Using c# and not xaml

Thanks.
 
R

RobinS

Grid grid = new Grid();

//do this for each column
ColumnDefinition coldef = new ColumnDefinition();
coldef.Width = GridLength.Auto;
grid.ColumnDefinitions.Add(coldef);

//do this for each row
RowDefinition rowdef = new RowDefinition();
rowdef.Height = GridHeigt.Auto;
grid.RowDefinitions.Add(rowDef);

TreeView tree = new TreeView();
grid.Children.Add(tree);
//put it in column 0, row 0
Grid.SetColumn(tree,0);
Grid.SetRow(tree,0);

Just out of curiousity, do you actually *own* any WPF books? If so, which
ones?

Robin S.
 
L

Linda Liu [MSFT]

Hi Moondaddy,

I agree with Robin. We could call the SetRow or SetColumn method of the
Grid class to set the value of the Row/Column attached property to a given
UIElement.

For more information on the System.Windows.Controls.Grid class, you may
visit the following link.

'Grid Class'
http://msdn2.microsoft.com/en-us/library/system.windows.controls.grid.aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Moondaddy,

I agree with Robin. We could call the SetRow or SetColumn method of the
Grid class to set the value of the Row/Column attached property to a given
UIElement.

For more information on the System.Windows.Controls.Grid class, you may
visit the following link.

'Grid Class'
http://msdn2.microsoft.com/en-us/library/system.windows.controls.grid.aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chris Dunaway

Just out of curiousity, do you actually *own* any WPF books? If so, which

I just finished reading "Windows Presentation Foundation
Unleashed" (Sams) by Adam Nathan. It's a very easy read, and he
explains things well. Plus, it's printed in full color so the code
samples show proper VS syntax coloring and the images are color as
well.

Chris
 
R

RobinS

Chris Dunaway said:
I just finished reading "Windows Presentation Foundation
Unleashed" (Sams) by Adam Nathan. It's a very easy read, and he
explains things well. Plus, it's printed in full color so the code
samples show proper VS syntax coloring and the images are color as
well.

Chris

Yes, I read that, but I liked Petzold's book better (although it was more
complicated).

I just wondered if moondaddy had any, because he posts tons of questions
about WPF, and some of them (like this one) seem like they would be easy to
look up.

Robin S.
 
M

moondaddy

Thanks Robin,

this was very helpful.


RobinS said:
Grid grid = new Grid();

//do this for each column
ColumnDefinition coldef = new ColumnDefinition();
coldef.Width = GridLength.Auto;
grid.ColumnDefinitions.Add(coldef);

//do this for each row
RowDefinition rowdef = new RowDefinition();
rowdef.Height = GridHeigt.Auto;
grid.RowDefinitions.Add(rowDef);

TreeView tree = new TreeView();
grid.Children.Add(tree);
//put it in column 0, row 0
Grid.SetColumn(tree,0);
Grid.SetRow(tree,0);

Just out of curiousity, do you actually *own* any WPF books? If so, which
ones?

Robin S.
----------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top