[WPF] create a custom list item panel

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.