2012-02-12 54 views
4

ListBox開始滾動時是否觸發事件?檢測ListBox的滾動事件?

我目前有以下代碼允許從列表框中無縫拖放。

<ListBox.ItemsPanel> 
        <ItemsPanelTemplate> 
         <StackPanel Orientation="Horizontal" Margin="-5" /> 
        </ItemsPanelTemplate> 
       </ListBox.ItemsPanel> 
       <ListBox.ItemTemplate > 
        <DataTemplate> 

         <Image ManipulationStarted="ListImage_ManipulationStarted" Tag="{Binding selfReference}" x:Name="ListImage" Margin="2.5" Stretch="Fill" Source="{Binding thumbnailURL}" Height="64" Width="64"/> 

        </DataTemplate> 
       </ListBox.ItemTemplate> 

      </ListBox> 

然後在代碼

private void ListImage_ManipulationStarted(object sender, ManipulationStartedEventArgs e) 
     { 
      if (dImage == null) 
      { 
       SoundEffectModel selectedModel = (sender as Image).Tag as SoundEffectModel; 
       int newIndex = listBoxSource.Items.IndexOf(selectedModel); 
       if (newIndex != -1) 
       { 
        listBoxSource.SelectedIndex = newIndex; 
       } 
      } 
     } 

我然後複製在列表框中選擇的項目,並直接將其在選定項目的當前位置。一切運作良好。

但是,如果用戶開始滾動listBox而不是拖動應用程序周圍的項目,重複的圖像位於listBox的頂部,它看起來不專業。只要用戶擡起手指,重複的項目就會被刪除,因爲我可以檢測到manipulateComplete事件,並意識到該項目位於「錯誤的地方」。

當滾動開始而不是等待manipulateComplete事件時,有沒有辦法讓我刪除該項目?


Related問題的context

回答

2

沒有,沒有一個事件時,會引發ListBox卷軸,但是,你可以找到ScrollViewer這是ListBox模板中和處理髮生的ValueChanged事件滾動開始。

您可以找到滾動條如下:

/// <summary> 
/// Searches the descendants of the given element, looking for a scrollbar 
/// with the given orientation. 
/// </summary> 
private static ScrollBar GetScrollBar(FrameworkElement fe, Orientation orientation) 
{ 
    return fe.Descendants() 
      .OfType<ScrollBar>() 
      .Where(s => s.Orientation == orientation) 
      .SingleOrDefault(); 

} 

這使用LINQ到Visual樹,如this blog post描述。

+0

Thanks @ColinE爲了讓答案更完整,我會這樣做:ScrollBar lbScrollBar = GetScrollBar(listbox,orientation).ValueChanged + = eventHandler;?還是還有其他一些步驟我需要做? – Bob 2012-02-12 16:04:33

+0

@Bob是的,抱歉忘了補充一點。使用上述方法找到ScrollBar,然後處理ValueCganged。確保ListBox Loaded事件先被觸發。 – ColinE 2012-02-12 16:07:43

+0

這種解決方案可能不適用於Windows Phone嗎?我在fe.Descendants上遇到錯誤? – Bob 2012-02-12 16:13:29