I want to build an ui with a dynamic grid. At least I think a grid is the way to go.
The controls of each cell should be the same, f.e. a TextBox
.
Each row can have a different number of cells and I thought about using the ColumnSpan
property to make the ui look like
First row, 2 cells
Second row, 3 cells
Third row, 1 cell
Each cell should be part of an f.e. ObservableCollection<MyCellClass>
and should have properties like Row
, Column
, Columnspan
, Text
.
I know how to build a dynamic grid but I don't know how to set the content of the cells via a template and how to databind the textbox.
Maybe my approach isn't the best and you got some other idea to solve my problem.
You may use a Grid as the
ItemsPanel
of an ItemsControl, and bind theItemsSource
property to your ObservableCollection.Assuming that your cell class looks like this:
the XAML may be written like shown below:
The following piece of code would initialize two cells of the ItemsControl:
Why not using something like this?
As underlying structure (viewmodel) for your grid you could use the following property:
Now you only need a
ListView
(or something similar), whoseItemTemplate
is again aListView
(or something similar). TheItemTemplate
of the innerListView
will be the visual representation ofItemVm
.Please note, that I'm describing an approach according to the MVVM pattern. So you will need a base class for the viewmodels. But you can find plenty of frameworks on the WWW.