Sypher_04
Sypher_04

Reputation: 121

WPF Wrapping content into maximum 2 columns. Evenly distributed

I am attempting to insert a panel into my WPF application that would have a few very specific behaviours:

1.) Wraps content evenly. Starting from the top left corner and running downward, before moving to the next column.

Example1

2.) Allows me to define a maximum number of columns to wrap to. For my purposes, this number would be between 1 and 3.

3.) Allows me to set an initial height, but it will also grow to accommodate additional items. (Only setting an initial height because my content won't wrap without it. If I leave it auto, it all comes out in a single column regardless of whether it fits on screen or not)

Example2

At this point, I have concluded that what I'm attempting do will require a custom panel, but I'd like to ensure before I begin that process (and learning how to do so) that I'm not missing a much simpler answer.

Upvotes: 3

Views: 1221

Answers (2)

Kilazur
Kilazur

Reputation: 3188

UniformGrid has a LayoutTransform property, which can be used to transform it in order to change the position/rotation of the elements inside. But it will also transform the content.

Some more tricks involving Setters on the types of the items inside your UniformGrid and the content can be transformed again to retain the desired "original" orientation.

You can learn more in this tutorial.

Alternatively, it seems that the Extended WPF Toolkit contains its own implementation of UniformGrid, with an Orientation property, the only problem being that it won't grow to accomodate the number of items; instead, it will obey to an arbitrary Columns property.

Then again, you may be able to change the value of this property each time you add a new item/resize your UniformGrid, but it will be some more manual work and may potentially lead to code behind, which could be seen as an issue if you're working in MVVM.

Upvotes: 0

Steven Rands
Steven Rands

Reputation: 5421

A WrapPanel can be set to wrap vertically, but you have no control over the number of columns.

A UniformGrid would offer you control over the number of columns, but wraps horizontally not vertically.

In short: you need a custom panel. The built-in ones do not offer the combination of features that you want.

Upvotes: 1

Related Questions