I’m trying to figure out if there is a way to dynamically add controls into another control (I know this is a bit vague…). My program is in C# 4.0, and WPF. Basically, I’m trying to create a datagrid but as opposed to having normal type ‘cells’ (ie text, hyperlink etc), I need each cell to hold a number of items. I figured that this wasn’t possible in the datagrid, so I’m trying to do the following: Using a stack panel, add a variable number of wrap panels. Each wrap panel will contain 7 grids, where each grid will hold the data I want (I’ll likely use some user control in place of the grid I think…)
An example of the code I have so far…
<StackPanel Height="559" HorizontalAlignment="Left" Margin="24,11,0,0" Name="tyStackPanel" VerticalAlignment="Top" Width="783">
<WrapPanel Height="100">
<Grid Width="100" Height="100">
</Grid>
</WrapPanel>
<WrapPanel Height="100">
</WrapPanel>
</StackPanel>
Is there a way to create a variable number of Wrap Panels though? (ie like you would have a variable number of rows in a datagrid)
Any help and suggestions is much appreciated!
P.S. Figure I should explain what I’m trying to achieve a bit better. I have a collection of items, each with 5 properties that I want displayed together. These items are grouped by Name (like a row in a data column) and a column header (which is not one of the 5 properties). I want to group the collection by (Name, ColumnHeader) pairs, and then in each “cell” display those 5 properties. In the way I’m trying to set it up above, there would be a WrapPanel per ‘Name’ and a Cell/Grid contained in it for each ColumnHeader.
WPF supports this very well with the
ItemsControland its various derived controls, one of which is theDataGrid, which actually does support the scenario you’re looking for.Basically, when you use an
ItemsControl,DataGridor one of these item controls, you bind theItemsSourceto whatever property holds your data items, and define theDataTemplates for each item, which can be any arbitrarily-complex block of XAML. ForDataGrid, you can swap a normal column for aDataGridTemplateColumn, which can, again, be as complex as you want.Check out the Data Templating Overview for, well, an overview.