微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 检测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的顶部,看起来不专业.一旦用户抬起他们的手指,就会删除重复的项目,因为我可以检测到manipulationComplete事件,并意识到该项目处于“错误的位置”.

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

contextRelated个问题:

解决方法

不,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到Visual Tree,如this blog post中所述.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐