How to dynamically populate a WPF grid using data binding?
-
05-07-2019 - |
Question
I am looking for a way to populate a single grid using data bindings.
The way I do this at the moment is by using an ItemsControl where each item is represented as a Grid with the columns that I need. The main reason I want a single grid is to make the widths of all columns line up.
Is there way for the Grid panel to be used with ItemsControl so that there is a single grid to contain all the items?
Or is there another solution?
Solution
You can make grid column widths "line up" across grids by using Grid.IsSharedSizeScope and SharedSizeGroup.
You simply need to set the Grid.IsSharedSizeScope property to true on the element that contains your grids, then set the SharedSizeGroup on the ColumnDefinitions you want to have the same width.
Both of the links above have examples.