views:

30

answers:

1

For example, I have a collection of integers 1 - 10. I want to dynamically display 4 (can be 5, 6, 7) columns in the datagrid in silverlight. How can I bind the collection to the datagrid to achieve the following?

        C1    C2    C3    C4
R1       1     2     3     4 

R2       5     6     7     8

R3       9     10

Cheers

+1  A: 

Unfortunately the answer is likely that the DataGrid is not the correct tool for this job. The DataGrid is designed to display tabular data, much like a spreadsheet, so wrapping isn't really part of the design.

Fortunately though, Silverlight (and the Silverlight Toolkit) do give you the tools you need to accomplish something like this. The ItemsControl is designed specifically for creating custom views of lists of data. Since the default Silverlight toolkit does not include a "WrapPanel", you'll also need to grab the excellent Silverlight Toolkit which does contain one.

You can then combine the ItemsControl and the WrapPanel to get a wrapping set of data.

<ItemsControl ItemsSource="{Binding NumbersList}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <controlsToolkit:WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
free-dom
I would still prefer a datagrid as I need to display empty cells in r3 c3, c4
Titan
Unfortunately, unless someone here knows of a clever hack, you aren't going to get the visual behavior you want from a DataGrid. Perhaps I'm looking at the problem all wrong though. Is your data source really a list of primitives?
free-dom