locked
Setting Grid.Row in code behind - how? RRS feed

  • Question

  • Hi,

    I have a bunch of Grid controls inside the main Grid (LayoutRoot) control. Those Grid controls are positioned inside the LayoutRoot's RowDefinitions (one for each child Grid control). Now, as of now I set a child Grid's Grid.Row="x" in the XAML markup. What I want to do, is to create a method in code behind which loops all the child Grid controls inside the LayoutRoot and sets the Grid.Row there.

    Is this even possible? I can't find anything that looks relevant on a Grid's properties/methods.

    Thanks in advance!

    Thursday, October 8, 2009 9:44 AM

Answers

  • Hi,

    Is this even possible?

    Yes. You can set it over SetValue method.

    yourControl.SetValue(Grid.RowProperty, <indexOfRow>);

     

    Thursday, October 8, 2009 9:48 AM

All replies

  • Hi,

    Is this even possible?

    Yes. You can set it over SetValue method.

    yourControl.SetValue(Grid.RowProperty, <indexOfRow>);

     

    Thursday, October 8, 2009 9:48 AM
  •  Worked like a charm! Thank you!

    Friday, October 9, 2009 4:48 AM
  • What if I have something like this :

    this.LayoutRoot.Children.Add(new Image() { Width = 800, Height = 600, Stretch = System.Windows.Media.Stretch.Fill, Source = wb, Visibility = System.Windows.Visibility.Visible });

     

    Where do I set the Grid.Row in there as Grid doesn't exist and SetValue isn't available.
    I know I can create the control first then add it to the collection but I just wanted to know if there's a way like that

    Wednesday, February 22, 2012 8:06 AM
  • i think you cant?

    or have you figured out how??

    Tuesday, March 27, 2012 10:32 AM
  • this.LayoutRoot.Children.Add(new Image() { Width = 800, Height = 600, Stretch = System.Windows.Media.Stretch.Fill, Source = wb, Visibility = System.Windows.Visibility.Visible });

    var image = new Image {Your settings here};

    image.SetValue(Grid.RowProperty, 4);

    this.LayoutRoot.Children.Add(image);

    Tuesday, March 27, 2012 11:36 AM
  • this.LayoutRoot.Children.Add(new Image() { Width = 800, Height = 600, Stretch = System.Windows.Media.Stretch.Fill, Source = wb, Visibility = System.Windows.Visibility.Visible });

    var image = new Image {Your settings here};

    image.SetValue(Grid.RowProperty, 4);

    this.LayoutRoot.Children.Add(image);

    Indeed jackbond but not the way I'm adding the new image and do you know why Grid.Row can't be set in such way ?

    Tuesday, March 27, 2012 6:51 PM
  • Indeed jackbond but not the way I'm adding the new image and do you know why Grid.Row can't be set in such way ?

    Grid.Row is not a property of the image class, but rather an attached property that the Grid looks for when laying out its children. Attached properties are really one of the beautiful features of XAML/WPF/Silverlight.

    Wednesday, March 28, 2012 1:28 AM
  • Indeed jackbond but not the way I'm adding the new image and do you know why Grid.Row can't be set in such way ?

    Grid.Row is not a property of the image class, but rather an attached property that the Grid looks for when laying out its children. Attached properties are really one of the beautiful features of XAML/WPF/Silverlight.

    Thank you for the clarification !
    So in a sence we're loosing the beauty for simplicity of instantiation.

    3 lines instead of 1

     

    Wednesday, March 28, 2012 2:42 PM
  • So in a sence we're loosing the beauty for simplicity of instantiation.

    3 lines instead of 1

    I actually don't see any beauty at all in the one line. When I set a break point on that line, what am I breaking on? And if you want to see the horrificness of systems without attached properties, download the android or iPhone SDKs. And if you really want it on one line, just remove the carriage returns after the semi-colons. :)

    Wednesday, March 28, 2012 7:39 PM
  • I actually don't see any beauty at all in the one line. When I set a break point on that line, what am I breaking on? And if you want to see the horrificness of systems without attached properties, download the android or iPhone SDKs. And if you really want it on one line, just remove the carriage returns after the semi-colons. :)

    Ok.

    Friday, March 30, 2012 7:08 PM