5

Is there an event fired when a ListBox begins to scroll?

I currently have the following code to allow for a seamless drag and dropping from a 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>

And then in the code

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;
                }
            }
        }

I then duplicate the selected item in the listbox, and place it directly over the current location of the selected item. All works well.

However, if the user begins to scroll the listBox instead of draging the item around the application, the duplicated image sits ontop of the listBox and it looks unprofessional. As soon as the user lifts their finger, the duplicated item is deleted, because I can detect the manipulationComplete event, and realize that the item is in the "wrong place".

Is there a way for me to delete the item when the scrolling begins instead of waiting for the manipulationComplete event?


Related questions for context:

1 Answer 1

2

No, there is not an event fired when the ListBox scrolls, however, you can locate the ScrollViewer which is within the ListBox template and handle the ValueChanged event which occurs as soon as scrolling starts.

You can locate the scrollbar as follows:

/// <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();

}

This uses Linq to Visual Tree, as described in this blog post.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @ColinE To make the answer more complete, would I do : ScrollBar lbScrollBar = GetScrollBar(listbox, orientation).ValueChanged+= eventHandler;? Or is there some other step I need to do?
@Bob yes, sorry forgot to add that. Use the above method to find the ScrollBar then handle the ValueCganged. Make sure the ListBox Loaded event has fired first.
is it possible that this solution does not work for the windows phone? I'm getting an error on fe.Descendants?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.