tags:

views:

68

answers:

2

Given this XAML:

<StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Margin="5">

How can I assign the Grid.Row and Grid.Column attributes in (C#/VB) code?

StackPanel stackPanel = new StackPanel();
stackPanel.???
+6  A: 

I think this is what you want:

    MyStackPanel.SetValue(Grid.RowProperty, 1);
    MyStackPanel.SetValue(Grid.ColumnProperty, 2);

Hope this helps

Anthony
Looks simple now that I see the code. Thanks.
scottmarlowe
+3  A: 

You should be able to do the following, where 'myGrid' is the name of your Grid control.

StackPanel stackPanel = new StackPanel();

Grid.SetColumn(stackPanel, 0);
Grid.SetRow(stackPanel, 1);

myGrid.Children.Add(stackPanel);
Richard C. McGuire
This works also, but Anthony gave me the first answer so he gets the accept. Thanks for the reply.
scottmarlowe
From my experience Rich's answer is more typical WPF/Silverlight code as it reads more like the XAML (and I've always thought is easier to discover).
WPCoder
I actually looked it up and the static methods on the Grid class actually call into the given UIElement's SetValue method. Obvsiouly both work, just comes down to personal preference.
Richard C. McGuire