While looking at the TagCloud below I thought, there are some things that can be done better. The article on the original TagCloud can be found here.Like:
- A middle alignment for all items in a row
- An equal divided space between items in a row
So what I did is take the original code from the WrapPanel and did some refactoring first. I changed the ArrangeOverride method to become a two phase step. The first step is to determine which items become in which row. I put those items in a two-dimensional collection. The next step is the actual arranging of the elements. One of the pieces of code for the arrange does determine which element is the largest in Height in a specific row. Besides this it also measures the total width of the elements in the row which is use to calculate the space needed between the elements.
1 double largestHeightInRow = elementsRow.Max(element => element.DesiredSize.Height); 2 double widthOfRow = elementsRow.Sum(element => element.DesiredSize.Width); 3 double betweenElements = (finalSize.Width-widthOfRow)/(elementsRow.Count + 1);
This all combined results in the following TagCloud.
The updated source can be found here.