Ashley Davis
Ashley Davis

Reputation: 10040

How to dynamically populate a WPF grid using data binding?

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?

Upvotes: 2

Views: 1849

Answers (3)

Pavel Minaev
Pavel Minaev

Reputation: 101555

Why not just use DataGrid?

Upvotes: 3

Charlie
Charlie

Reputation: 15227

Use a ListView with a GridView embedded in the ListView.View property. There are examples of this here and here.

Upvotes: 2

John Myczek
John Myczek

Reputation: 12256

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.

Upvotes: 2

Related Questions