WPF Listbox/listview/datagrid list Scrolling and virtualization

Source: Internet
Author: User

The scroll mode of the listbox is divided into pixel scrolling and unit scrolling

Set by the additional property Scrollviewer.cancontentscroll of the listbox. So the default template for the listbox contains the contents of the Scrollviewer,scrollviewer under the store list

    <focusvisualstyle= "{x:null}">        < snapstodevicepixels = "{TemplateBinding uielement.snapstodevicepixels}    "/>    </ScrollViewer>

While Cancontentscroll,true supports logical units (Item), false supports physical units (pixels). The source code is as follows:

    /// <summary>    ///Gets or sets a value that indicates whether the element is supported<see cref= "T:System.Windows.Controls.Primitives.IScrollInfo"/>interface allows scrolling. /// </summary>    /// <returns>    ///   <see langword= "true"/>if<see cref= "T:System.Windows.Controls.ScrollViewer"/>performing a scrolling operation makes the logical unit;<see langword= "false"/>if<see cref= "T:System.Windows.Controls.ScrollViewer"/>performing a scrolling operation makes the physical unit aspect. ///The default value is<see langword= "false"/>. /// </returns>     Public BOOLCancontentscroll {Get      {        return(BOOL) This.      GetValue (Scrollviewer.cancontentscrollproperty); }      Set      {         This.      SetValue (scrollviewer.cancontentscrollproperty, value); }    }

Rolling

1, Pixel Scrolling (physical unit) Scrollviewer.cancontentscroll=false

By looking at the source code, we can learn that the default value of Cancontentscroll is false. So list listbox/listview/datagrid default pixel scrolling

    /// <summary>    ///Identification<see cref= "P:System.Windows.Controls.ScrollViewer.CanContentScroll"/>dependency property. /// </summary>    /// <returns>    ///   <see cref= "P:System.Windows.Controls.ScrollViewer.CanContentScroll"/>The identifier for the dependency property. /// </returns>[Commondependencyproperty] Public Static ReadOnlyDependencyProperty Cancontentscrollproperty = dependencyproperty.registerattached (nameof (Cancontentscroll),typeof(BOOL),typeof(ScrollViewer), (PropertyMetadata)NewFrameworkpropertymetadata (Booleanboxes.falsebox));
[friendaccessallowed]Internal Static classbooleanboxes {Internal Static ObjectTruebox = (Object)true; Internal Static ObjectFalsebox = (Object)false; Internal Static ObjectBox (BOOLvalue) {      if(value)returnBooleanboxes.truebox; returnBooleanboxes.falsebox; }  }

The advantage of pixel scrolling: smooth-------------because the pixel is scrolled.

The disadvantage of pixel scrolling: consumption performance-each item in the list is calculated with a width-to-height specific value, and is calculated at the moment of scrolling. If there are too many in the list, it is quite a card.

2. List item scrolling (logical unit) scrollviewer.cancontentscroll= "True"

Follow item aspect as a scrolling unit.

When the unit scrolls, the list will only scroll to a full item and will not have an item showing only half the case.

--based on the above situation, when the list just has a itema only display part, because the business needs, the settings scroll to Itema, the entire list will move the position to make the Itema full display.

Virtualization

By Virtualizingpanel, set the list Listbox/listview/datagrid whether to turn on virtualization

Virtualizingpanel Other properties are:

virtualizingpanel.scrollunit= "Pixel"--Virtualization scrolling unit (pixels/unit)

Virtualizingpanel.isvirtualizing= "True"--whether the virtual

Virtualizingpanel.virtualizationmode= "Recycling"

virtualizingpanel.cachelengthunit= "Item"--cache unit

Virtualizingpanel.cachelength= "20,20"-number of up and down caches

Turn on virtualization: Why do I need to set scrollviewer.cancontentscroll= "True"?

Because scrollviewer.cancontentscroll= "False" is a physical unit, that is, pixel scrolling, when there is a lot of data, even if the virtualization is turned on, because the computation too much performance, the interface is as stuck.
So set scrollviewer.cancontentscroll= "True"

Setting up Virtualization

Set virtualizingpanel.isvirtualizing= "True" to turn on virtualization

virtualizingpanel.scrollunit= "Pixel" pixel, virtualizingpanel.scrollunit= "Item" list item

Note that virtualization also has a physical unit and a logical unit. So after setting scrollviewer.cancontentscroll= "True", the scrolling unit settings are transferred to Virtualizingpanel.scrollunit .

Note:

The Virtualizingpanel.scrollunit and Scrollviewer.cancontentscroll have the same set of scrolling units.

When the total height of the items displayed in the list is not equal to the list display height, that is: Itemheight*n! =listbox.height, it is best to use virtualizingpanel.scrollunit= "Item".

Sets the virtual unit as a unit item, scrolling automatically to a complete item instead of scrolling to the part of the item.

Virtualizingpanel.scrollunit= "Pixel" will appear as shown on the right:

Case: Pixel scrolling and list item scrolling when virtualizing

Scrollviewer.cancontentscroll= "true" virtualizingpanel.isvirtualizing= "true" virtualizingpanel.scrollunit= "Pixel "
Scrollviewer.cancontentscroll= "true" virtualizingpanel.isvirtualizing= "true" virtualizingpanel.scrollunit= "Item"

WPF Listbox/listview/datagrid list Scrolling and virtualization

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.