c# - 检测列表框的滚动事件?

标签 c# windows-phone-7 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;
                }
            }
        }

然后我复制列表框中的所选项目,并将其直接放在所选项目的当前位置上。一切正常。

但是,如果用户开始滚动列表框而不是在应用程序周围拖动项目,则重复的图像会位于列表框的顶部并且看起来不专业。一旦用户抬起手指,重复的项目就会被删除,因为我可以检测到 manipulationComplete 事件,并意识到该项目位于“错误的位置”。

有没有办法让我在滚动开始时删除项目而不是等待 manipulationComplete 事件?


Related关于 context 的问题:

最佳答案

不,ListBox 滚动时不会触发事件,但是,您可以在 ListBox 模板中找到 ScrollViewer并处理滚动开始后立即发生的 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 to Visual Tree,如 this blog post 中所述.

关于c# - 检测列表框的滚动事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9248171/

相关文章:

c# - 无法创建列表对象数组

c# - 是否可以在 Windows Phone 7 上实现 ping?

unit-testing - Windows Phone 7 的 Silverlight 单元测试框架中是否有等效的 @After 或 @AfterClass 注释

c# - WPF - 从触发器访问子项

c# - 如何遍历列表框中的项目然后删除这些项目?

c# - 检查 XML 节点是否具有 Linq C# 的属性?

c# - PM 给我 ERROR : Type is not resolved for member. .on any migration command

C# "validate"绑定(bind)到表单的对象的最佳方法

c# - WP7 应用程序中 GUID 的用途是什么?

wpf - 多选 WPF 列表框通过单击进行多项选择