I have a WrapPanel, And I want to specify the Max number of its columns. So, for example, when my Collection \"ObjectCollection\" (binded to this WrapPanel) contains only 4 elem
Basically you're going to need to create a custom Panel
for yourself... now don't get despondent... it's not that difficult. To start with, please take a look at the posts that I have provided links for that explain how to create a custom Panel
:
How to create a Custom Layout Panel in WPF
Creating Custom Panels In WPF
Ok, so now that you know a bit more about creating custom Panel
s, we can continue... here's what you're going to need:
private int columnCount;
private double leftColumnEdge, rightColumnEdge, columnWidth;
public int ColumnCount
{
get { return columnCount; }
set
{
if (value < 1) value = 1;
columnCount = value;
}
}
This property would be used where you declare your Panel
in Resources
:
Note that you will need to declare it inside an ItemsPanelTemplate
object because that is what the ItemsPanel
property expects:
Now back to the Panel
... here is a helper method that I call from the MeasureOverride
and ArrangeOverride
methods:
private void UpdateColumns(int currentColumn, Size finalSize)
{
leftColumnEdge = (finalSize.Width / ColumnCount) * currentColumn;
rightColumnEdge = (finalSize.Width / ColumnCount) * (currentColumn + 1);
columnWidth = rightColumnEdge - leftColumnEdge;
}
Unfortunately for you, I cannot provide you with a complete example because my custom Panel
s are all tied into a base AnimatedPanel
class with lots of additional functionality. However, you only need to create the MeasureOverride
and ArrangeOverride
methods to complete this Panel
. If you just think logically about it, it's really not that difficult.