none
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.

    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

    TJANA

    Friday, May 24, 2019 11:14 AM

Answers

  • Hi,

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

    Which is the UniformGrid.

    https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/panels-overview#Panels_derived_elements

    http://www.blackwasp.co.uk/WPFUniformGrid.aspx

    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:

    https://rachel53461.wordpress.com/2011/09/17/wpf-itemscontrol-example/

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

    https://social.technet.microsoft.com/wiki/contents/articles/33327.wpf-wrappanel-itemspanel-last-of-line.aspx


    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
    Moderator