tags:

views:

19

answers:

1

I am trying to display an array as grid of numbers, with multiple columns and multiple rows.

I set up a ListView with multiple columns bound to my array, but the elements are repeated in each column, so that the data looks like:

-------+--------+-------
First  | First  | First 
Second | Second | Second

rather than:

-------+--------+-------
First  | Second | Third
Fourth | Fifth  | Sixth

How can I set up the ListView to display the data as in the second example?

+1  A: 

The default items panel for a ListView is a vertically-oriented StackPanel. Since each number comes as a separate item for the ListView and each row is bound to an item, you can't have different columns in one row bind to different items in the bound collection.

However, you can have a ListBox or a ListView that uses WrapPanel as items panel. If you use ListView, you'll have single-column items, and the wrap panel will take care of the proper items layout. However, I'd actually stick with ListBox, unless you need some ofthe advanced ListView functionality.

To change the items panel, use the ItemsPanel property. Here's an example in XAML:

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel />
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

The alternative is to change the ControlTemplate for the ListView and use the IsItemsHost property (specify IsItemsHost="True") on the panel that you want to act as the items panel.

Edit: Updated to use the proper terminology. When I said item container, I meant items panel. That's what happens when answering from an iPhone.

Franci Penov
How would I do that (changing the ItemContainer I mean)?
samoz