2012-02-12 50 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