加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 检测ListBox的滚动事件?

发布时间:2020-12-15 17:23:36 所属栏目:百科 来源:网络整理
导读:ListBox开始滚动时是否触发了一个事件? 我目前有以下代码,允许从列表框中无缝拖放. ListBox.ItemsPanel ItemsPanelTemplate StackPanel Orientation="Horizontal" Margin="-5" / /ItemsPanelTemplate /ListBox.ItemsPanel ListBox.ItemTemplate DataTemplat
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事件?

context的Related个问题:

解决方法

不,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中所述.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读