开发者

Binding a collection in silverlight

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 t开发者_运维知识库he datagrid to achieve the following?

        C1    C2    C3    C4
R1       1     2     3     4 

R2       5     6     7     8

R3       9     10

Cheers


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜