Summary of WP7 listbox

Source: Internet
Author: User

1. Configure ListBox In The XAML file and add ListBoxItem to the corresponding ListBox In the CS file. If the number of listboxitems is large (more than one screen is displayed), when you drag the end of ListBox, go to the new page and return to the page. The ListBox is blank.

XAML file:

<Grid>    <ListBox x:Name="MyListBox"  /></Grid>

CS file:

private void AddItems(){    for (int i = 0; i < 50; i++)    {        ListBoxItem item = new ListBoxItem();        TextBlock textBlock = new TextBlock();        textBlock.Text = i.ToString();        item.Content = textBlock;        MyListBox.Items.Add(item);    }}

Cause Analysis: after the original page is returned, the VerticalOffset and ScrollableHeight values of ListBox are increased (beyond the screen height ), therefore, when the user returns to the original page and does not drag The ListBox up, the data content cannot be viewed.

Current solution: Modify the XAML file and customize the Template of ListBox.

<Grid>    <ListBox x:Name="MyListBox"  >        <ListBox.Template>            <ControlTemplate TargetType="ListBox">                <ScrollViewer BorderBrush="{TemplateBinding BorderBrush}"  BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"  Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}" >                    <Grid>                        <ItemsPresenter/>                    </Grid>                </ScrollViewer>            </ControlTemplate>        </ListBox.Template>    </ListBox></Grid>

 

2. Add and drag in ListBox To the bottom of ListBox for data loading (paging loading ):

Common Methods:

1) traverse the subcontrol of ListBox and find the self-contained ScrollViewer in ListBox;

?
    // Obtain the child Typepublic static T FindChildOfType<T>(DependencyObject root) where T : class{ var queue = new Queue<DependencyObject>(); queue.Enqueue(root); while (queue.Count > 0) { DependencyObject current = queue.Dequeue(); for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--) { var child = VisualTreeHelper.GetChild(current, i); var typedChild = child as T; if (typedChild != null) { return typedChild; } queue.Enqueue(child); } } return null; }

2) If ScrollViewer. VerticalOffset> = ScrollViewer. ScrollableHeight, it has been dragged to the bottom of ListBox;

For simplicity, you do not need to traverse ListBox to obtain ScrollViewer. Modify the Template in the XAML file and add the drag method of ScrollViewer.

XAML file:

<Grid>    <ListBox x:Name="MyListBox"  >        <ListBox.Template>            <ControlTemplate TargetType="ListBox">                <ScrollViewer BorderBrush="{TemplateBinding BorderBrush}"  
                  BorderThickness="{TemplateBinding BorderThickness}"
                  Background="{TemplateBinding Background}"
                  Foreground="{TemplateBinding Foreground}"
                  Padding="{TemplateBinding Padding}"
                  ManipulationCompleted="ListBox_ManipulationCompleted"> <Grid> <ItemsPresenter/> </Grid> </ScrollViewer> </ControlTemplate> </ListBox.Template> </ListBox></Grid>

CS file:

Private void ListBox_ManipulationCompleted (object sender, ManipulationCompletedEventArgs e)
{
ScrollViewer scrollViewer = sender as ScrollViewer;
If (scrollViewer = null)
{
Return;
}

// If it is dragged down and already at the bottom of ListBox
If (e. TotalManipulation. Translation. Y <0 & scrollViewer. VerticalOffset> = scrollViewer. ScrollableHeight) {// do something }}

 

3. display the latest data by page in ListBox:

Basic Idea: Modify the VerticalOffset of ScrollViewer in ListBox, record VerticalOffset before loading by PAGE, and then adjust the VerticalOffset value as needed after loading.

In practice, it is found that the ScrollableHeight of ScrollViewer in ListBox does not change in time after the page is loaded. Therefore, adjusting the VerticalOffset value does not work. You still need to drag the screen to see the latest loaded data.

Due to insufficient understanding of the internal mechanisms of ListBox and ScrollViewer, we can only monitor whether the ScrollableHeight of ScrollViewer changes in LayoutUpdated. If yes, adjust the VerticalOffset value of ScrollViewer immediately.

XAML file:

<Grid>    <ListBox x:Name="MyListBox"  >        <ListBox.Template>            <ControlTemplate TargetType="ListBox">                <ScrollViewer BorderBrush="{TemplateBinding BorderBrush}"                    BorderThickness="{TemplateBinding BorderThickness}"                   Background="{TemplateBinding Background}"                    Foreground="{TemplateBinding Foreground}"                   Padding="{TemplateBinding Padding}" 
                  LayoutUpdated="ListBox_LayoutUpdated"                  ManipulationCompleted="ListBox_ManipulationCompleted"> <Grid> <ItemsPresenter/> </Grid> </ScrollViewer> </ControlTemplate> </ListBox.Template> </ListBox></Grid>

CS file:

Private double currentVerticalOffset = 0.0d;
Private ScrollViewer myScrollViewer = null;
Private double scrollableHeight = 0.0d;

Private void ListBox_ManipulationCompleted (object sender, ManipulationCompletedEventArgs e) {ScrollViewer scrollViewer = sender as ScrollViewer; if (scrollViewer = null) {return ;}

If (myScrollViewer = null)
{
MyScrollViewer = scrollViewer;
} // If you are dragging down and already at the bottom of the ListBox, if (e. TotalManipulation. Translation. Y <0 & scrollViewer. VerticalOffset> = scrollViewer. ScrollableHeight ){
// Record the current offset value
CurrentVerticalOffset = scrollViewer. VerticalOffset; // do something }}

Private void ListBox_LayoutUpdated (object sender, EventArgs e)
{
If (myScrollViewer! = Null)
{
If (myScrollViewer. ScrollableHeight> scrollableHeight)
{
If (scrollableHeight> 0)
{
// Set the vertical position. drag down the actual height of The ListBox.
MyScrollViewer. ScrollToVerticalOffset (currentVerticalOffset + this. MyListBox. ActualHeight );
}

ScrollableHeight = myScrollViewer. ScrollableHeight;
}
}
}

Http://www.cnblogs.com/IronSword/archive/2012/04/12/2444210.html

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.