ListView設置滾動條位置的幾種方式


1、通過listview.ScrollIntoView()方法實現:

  滾動到頂端:listview.ScrollIntoView(listview.Items[0]);

  滾動到低端:

    int count = listview.Items.Count;
    int index = count - 1; listview.ScrollIntoView(listview.Items[index]);

   滾動到指定位置:listview.ScrollIntoView(listview.Items[index]);

 

2、通過控制listview內部的ScrollView控件實現:  

    Border bor = VisualTreeHelper.GetChild(listview, 0) as Border;    //listview內部第一層為Border容器
    ScrollViewer sview = bor.Child as ScrollViewer;            //border內部的child即為scrollview,正是我們接下來操作需要的控件

 OK!接下來可以實現之前的功能:

    滾動到頂端:sview.ScrollToTop();

    滾動到低端:sview.ScrollToBottom();

    滾動到指定位置:sview.ScrollToVerticalOffset(positin);         //position為垂直方向滾動的高度,同之前的index不同   

 

ScrollView其他相關的方法:  

 1 //
 2 // 摘要:
 3 // Scrolls the System.Windows.Controls.ScrollViewer content downward by one page.
 4 public void PageDown();
 5 //
 6 // 摘要:
 7 // Scrolls the System.Windows.Controls.ScrollViewer content to the left by one page.
 8 public void PageLeft();
 9 //
10 // 摘要:
11 // Scrolls the System.Windows.Controls.ScrollViewer content to the right by one
12 // page.
13 public void PageRight();
14 //
15 // 摘要:
16 // Scrolls the System.Windows.Controls.ScrollViewer content upward by one page.
17 public void PageUp();
18 //
19 // 摘要:
20 // Scrolls vertically to the end of the System.Windows.Controls.ScrollViewer content.
21 public void ScrollToBottom();
22 //
23 // 摘要:
24 // Scrolls vertically to the end of the System.Windows.Controls.ScrollViewer content.
25 public void ScrollToEnd();
26 //
27 // 摘要:
28 // Scrolls vertically to the beginning of the System.Windows.Controls.ScrollViewer
29 // content.
30 public void ScrollToHome();
31 //
32 // 摘要:
33 // Scrolls the content within the System.Windows.Controls.ScrollViewer to the specified
34 // horizontal offset position.
35 //
36 // 參數:
37 // offset:
38 // The position that the content scrolls to.
39 public void ScrollToHorizontalOffset(double offset);
40 //
41 // 摘要:
42 // Scrolls horizontally to the beginning of the System.Windows.Controls.ScrollViewer
43 // content.
44 public void ScrollToLeftEnd();
45 //
46 // 摘要:
47 // Scrolls horizontally to the end of the System.Windows.Controls.ScrollViewer content.
48 public void ScrollToRightEnd();
49 //
50 // 摘要:
51 // Scrolls vertically to the beginning of the System.Windows.Controls.ScrollViewer
52 // content.
53 public void ScrollToTop();
54 //
55 // 摘要:
56 // Scrolls the content within the System.Windows.Controls.ScrollViewer to the specified
57 // vertical offset position.
58 //
59 // 參數:
60 // offset:
61 // The position that the content scrolls to.
62 public void ScrollToVerticalOffset(double offset);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM