2

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.

2
  • 1
    You really need to read up on ItemsControl and data templates. Commented Apr 27, 2012 at 17:16
  • Yea, I'm definitely not an expert on this stuff... at all... Commented Apr 27, 2012 at 17:21

1 Answer 1

2

WPF supports this very well with the ItemsControl and its various derived controls, one of which is the DataGrid, which actually does support the scenario you're looking for.

Basically, when you use an ItemsControl, DataGrid or one of these item controls, you bind the ItemsSource to whatever property holds your data items, and define the DataTemplates for each item, which can be any arbitrarily-complex block of XAML. For DataGrid, you can swap a normal column for a DataGridTemplateColumn, which can, again, be as complex as you want.

Check out the Data Templating Overview for, well, an overview.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Avner, thanks for your reply. Where would I find an ItemsControl, as I don't see it in the toolbox (I'm next to useless with WPF....)
I've never used the toolbox, I have to admit. I've found that writing the XAML manually is much, much faster. Especially with Visual Studio 2010 being so hopelessly slow rending the XAML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.