Barbacamanitu
Barbacamanitu

Reputation: 61

WPF - How to list Objects in a DataGrid or ListView in a strange way?

I have an observable collection with some objects in it. These objects have 4 strings a piece in them. I want to be able to display this data in a datagrid, but not like you'd think. If I let it auto generate columns, then I get a column for each field. What I need is a set amount of columns, for my items to be added as a custom template into each cell. It should start at the top left, and continue to the right, and restart at the end of every row.

For example: Let's say I have 10 items, and 3 columns. I should automatically have 3 rows with 3 columns a piece, and 1 item on the bottom row. These items will consist of 4 textblocks that display the data correctly.

I cannot figure out how to make a datagrid behave this way. I'm starting to think that another control may work better, but I'm unsure. If anyone can help with the datagrid, or point me in the right direction with another control, I'd really appreciate it.

Thanks in advance!

Upvotes: 0

Views: 86

Answers (1)

Joel Lucsy
Joel Lucsy

Reputation: 8706

Sounds like you're looking for WrapPanel. Although you would have to supply the items you want in the list individually, not part of the object. You could bind to a IEnumerable that iterates over your object list and then returns each string piecemeal.

public IEnumerable<String> SomeList
{
    get
    {
        foreach (var item in SomeOtherList)
        {
            yield return item.String1;
            yield return item.String2;
            yield return item.String3;
            yield return item.String4;
        }
    }
}

Upvotes: 1

Related Questions