How to add wpf control to particular grid row and cell during runtime?

前端 未结 2 1617
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 05:15

I have the following grid in my WPF \"Window\" (yes the class Window);


            
                 


        
相关标签:
2条回答
  • 2020-12-15 05:49

    The Grid.Row and Grid.Column properties are Attached Properties, and as such are not set like normal .net properties. The right way to set them from code is:

    Grid.SetRow(someLabel, 0);
    Grid.SetColumn(someLabel, 0);
    

    You should be able to do this before or after adding them to the Grid object's Children collection, but setting them before adding the control should prevent any possible flickering.

    0 讨论(0)
  • 2020-12-15 05:50
    • Create the grid (<yourGrid>) and the Row Definitions like you have done.
    • Create The Control (<yourcontrol>). Then Set The ColumnSpan and Row for the Grid:

      Grid.SetColumnSpan(<yourControl>, 3); Grid.SetRow(<yourControl>, 0);

    • Then add your control to the grid you have created

      <yourGrid>.Children.Add(<yourControl>);

    0 讨论(0)
提交回复
热议问题