When using a list control like ListBox, we all know that you can use its ItemsPanel dependency attribute to customize a panel to place list items in the list control. Apart from the several panels provided by the CLR library, we can use the panel we write as the container of the item list.
Let's take a look at the results.
How? What's better?
The Panel principle is as follows:
1. Send a class from the Panel class. I name it MyPanel.
2. Override the MeasureOverride method to calculate the size of all child elements.
3. Override the ArrangeOverride method to randomly generate X and Y coordinates for each sub-element, and then place the sub-element with the random coordinates.
4. In order to enable automatic formatting every time, I added a DispatcherTimer and published a SwapInterval attribute. The timer execution interval can be set for the caller, in seconds.
5. Think about it later. If the InvalidateArrange method is called every time to re-arrange child elements, it seems boring. It is better to make some animations between the re-arrangement. As a result, I made the Panel fade out before re-arranging the child elements. When the sub-elements are arranged on the panel, the "Fade in" effect is good. It's just a matter of animation processing the Opacity attribute.
Well, the overall idea is like this, and then we can use this panel on ItemsPanel of ListBox and other list controls.
Next I will post the code of the entire MyPanel class for your reference.
=====+ = Property DependencyProperty SwapIntervalProperty = DependencyProperty. register (, (), (MyPanel), PropertyMetadata (10d, PropertyChangedCallback (SwapIntervalChanged), = d sec = (= CoerceValCallback (DependencyObject d, dv = (dv <={ (Timer_Tick (=== TimeSpan. fromMilliseconds (+ = (sd, arg) ==== TimeSpan. fromSeconds (UIElement u (UIElement item maxX = finalSize. width-maxY = finalSize. height-(maxX <= (maxY <= X = rand. next (, (Y = rand. next (, (item. arrange (
The code can be transplanted to Windows Phone or Windows StoreApp in the same principle.
There is no need to post other code, so as not to affect the reading of handsome guys, I just upload the entire project.
: Http://files.cnblogs.com/tcjiaan/MyLayoutPanel.zip