2011-04-25 62 views
1

我有一個拖放式框架,滾動支持已經存在(像素由像素滾動)。這適用於虛擬化未啓用的情況,但如果已啓用,則會出現混亂。
隨着滾動的邏輯是基於視口高度和按MSDN我們已經 -合併滾動 - 「像素由像素」+「項目的項目」

MSDN-如果CanContentScroll爲真,則ExtentHeight,ScrollableHeight,ViewportHeight和VerticalOffset屬性的值是項目數量。如果CanContentScroll爲false,則這些屬性的值是設備無關像素。另外,如果虛擬化已啓用,則ExtentHeight表示 - ScrollViewer和視口高度中的項目總數表示當前可見的項目數。

所以滾動力的工作,我想要的東西like--

ScrollToContent(ScrollViewer, CurrentMousePositionWRTScrollViewer) 
{ 
    if(ScrollViewer's Viewport Height is in terms of Pixel) 
{ 
----------Do Pixel by Pixel Scrolling -------- 
} 
else if(ScrollViewer's Viewport Height represents number of items visible) 
{ 
--------- Do Item by Item Scrolling --------- 
} 

我試圖把的「CanContentScroll =假」檢查,檢查已啓用虛擬化或沒有,但即使是力的工作,因爲我發現,即使CanContentScroll爲true也是如此Viewport高度dint表示可見項的數量,但它等於實際高度。然而,在另一個列表框中顯示 - 可見項目的數量。滾動

實際代碼 -

private void ScrollToContent(ScrollViewer scrollViewer, Point point) 
    { 
     double verticalScrollOffset = 0.0; 
     double scrollDifference = 30.0; 
     double scrollDefaultOffset = 40.0; 

     if (scrollViewer == null) return; 
     if (scrollViewer.ViewportHeight != scrollViewer.ExtentHeight) 
     { 
       if (scrollViewer.ViewportHeight - point.Y < scrollDifference) 
       { 
        // See if we need to scroll down 
        verticalScrollOffset = scrollDefaultOffset; 
       } 
       else if (point.Y < scrollDifference) 
       { 
        // See if we need to scroll up 
        verticalScrollOffset = -scrollDefaultOffset; 
       } 
       // Scroll up or down 
       if (verticalScrollOffset != 0.0) 
       { 
        verticalScrollOffset += scrollViewer.VerticalOffset; 
        if (verticalScrollOffset < 0.0) 
        { 
         verticalScrollOffset = 0.0; 
        } 
        else if (verticalScrollOffset > scrollViewer.ScrollableHeight) 
        { 
         verticalScrollOffset = scrollViewer.ScrollableHeight; 
        } 
        scrollViewer.ScrollToVerticalOffset(verticalScrollOffset); 
       } 
     } 
    } 

我在幻想,虛擬化是罪魁禍首,但檢查IsVirtualization財產後,我注意到,虛擬化是不是一個問題在這裏(它的兩個列表框真)..任何想法可能的情況是什麼?
的問題是 - 我有2名列表框(幾乎相同)在一個情況下,我得到 視口高度==項目數可見 但是在其他情況下 視口高度=實際高度 ..

什麼可能是可能的原因?

回答

0

Answer Actual Height Vs Viewport Height and Scrolling Issue

代碼:我以前選擇逐項在虛擬化的情況下,滾動到會藉此項目啓用

// Manage vertical scrolling. 
     if (scrollViewer.ViewportHeight != scrollViewer.ExtentHeight) 
     { 
      // NOTE :MSDN- If CanContentScroll is true, the values of the ExtentHeight, ScrollableHeight, ViewportHeight, and VerticalOffset 
      // properties are number of items. If CanContentScroll is false, the values of these properties are Device Independent Pixels. 
      // Also If Virtualization is Enabled then ExtentHeight Represents - TotalNumber of Items in ScrollViewer and Viewport Height 
      // Represents Count of Items currently visible. 
      if (scrollViewer.CanContentScroll == false) 
      { 
       if (scrollViewer.ViewportHeight - point.Y < scrollDifference) 
       { 
        // See if we need to scroll down 
        verticalScrollOffset = scrollDefaultOffset; 
       } 
       else if (point.Y < scrollDifference) 
       { 
        // See if we need to scroll up 
        verticalScrollOffset = -scrollDefaultOffset; 
       } 

       // Scroll up or down 
       if (verticalScrollOffset != 0.0) 
       { 
        verticalScrollOffset += scrollViewer.VerticalOffset; 

        if (verticalScrollOffset < 0.0) 
        { 
         verticalScrollOffset = 0.0; 
        } 
        else if (verticalScrollOffset > scrollViewer.ScrollableHeight) 
        { 
         verticalScrollOffset = scrollViewer.ScrollableHeight; 
        } 

        scrollViewer.ScrollToVerticalOffset(verticalScrollOffset); 
       } 
      } 
      else 
      { 
       if (scrollViewer != null) 
       { 
        // To ControlScrolling Speed - as we're doing Item By Item Scrolling now, it would be quite fast so add a time gap. 
        if (DateTime.Now.Subtract(lastScrollTime).TotalMilliseconds > 200) 
        { 
         if (scrollViewer.ActualHeight - point.Y < scrollDifference) 
         { 
          // See if we need to scroll down 
          verticalScrollOffset = scrollDefaultOffset; 
         } 
         else if (point.Y < scrollDifference) 
         { 
          // See if we need to scroll up 
          verticalScrollOffset = -scrollDefaultOffset; 
         } 

         if (verticalScrollOffset != 0.0) 
         { 
          // Scroll Content upwards 
          if (verticalScrollOffset < 0 && scrollViewer.VerticalOffset > 0) // Also check if there are any non visible elements in scrollviewer 
          { 
           scrollViewer.LineUp(); 
           lastScrollTime = DateTime.Now; 
          } 
          else if (verticalScrollOffset > 0 && 
           scrollViewer.VerticalOffset < scrollViewer.ExtentHeight - scrollViewer.ViewportHeight) 
          { 
           scrollViewer.LineDown(); 
           lastScrollTime = DateTime.Now; 
          } 
         } 
        } 
       } 
      } 
     } 
+0

Hi Rohit,即使虛擬化處於打開狀態並且CanContentScroll = True,您的代碼是否會逐個像素地滾動?你在哪裏連接了這個方法? SelectedItemChanged或滾動? – 2013-09-18 01:52:35

+0

獅子座,很抱歉,但我很久以前就有這個問題,我不再在這個項目上工作。別忘了。 – Rohit 2013-09-18 04:05:04

1
bool isVirtualizing = (bool)scrollViewer.GetValue(VirtualizingStackPanel.IsVirtualizingProperty); 
VirtualizationMode mode = (VirtualizationMode)scrollViewer.GetValue(VirtualizingStackPanel.VirtualizationModeProperty); 
+0

嗯謝謝!我認爲虛擬化是這裏的罪魁禍首,因爲滾動不起作用。 – Rohit 2011-04-25 14:30:25