how to create a grid with X number of rows and Y number of columns in run time with XAML and equally aligned vertically to a grid. add controls to that grid. RRS feed

  • Question

  • hello Experts,

    i need to create x rows and y columns in a grid.but that grid has to create dynamically(x and y will at run time) using XAML.

    grid has to be fixed size.controls has to be equally aligned in the grid.

    i tried a lot,finally got the required number of rows. but those are not equally aligned in a grid vertically.

    please help me to get the solution.

    Thanks in advance


    Friday, May 24, 2019 11:14 AM


  • Hi,

    The obvious candidate is a panel designed to have equal cells.

    Which is the UniformGrid.

    You can just add in loads of controls and it'll give you an equal number of rows and columns.

    Or you can set one or both of rows and columns

    <UniformGrid Columns="2" Rows="3"

    Another option would be to size the controls go into the cells and use a wrappanel. 

    When you have repeated WPF UI your first candidate should usually be some sort of an itemscontrol.

    You can set the itemspresenter to be any of those panels and template out data bound to the itemssource into ui.

    A more complete explanation of options:

    The sample for this article does a load more than just that and uses a wrappanel.

    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Marked as answer by TJANA Monday, May 27, 2019 4:13 AM
    Saturday, May 25, 2019 2:36 PM