当 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 事件?
最佳答案
不,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/