0

Currently, I have a ListBox which is used to carry out a drag/drop operation. While this does work an error has occurred where if an item in the list is selected, then the user cannot use the vertical scrollbar with their cursor clicking on it. The user can only move this scrollbar using a mouse wheel.

From what I understand, I think it tries to count the scrollbar as a SelectedItem which is maybe why it cannot move properly, but I'm still unsure as to why this is occurring.. Below is the relevant code, any help on solving this problem would be greatly appreciated.

XAML

<ListBox x:Name="WriteListBox"
           Height="326"
           Width="190"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           Margin="428,25,0,0"
           ScrollViewer.HorizontalScrollBarVisibility="Disabled"
           ScrollViewer.VerticalScrollBarVisibility="Auto"
           PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"
           PreviewMouseMove="ListBox_PreviewMouseMove"
           ItemTemplate="{StaticResource ModelVariableWriteTemplate}" />

Code-Behind

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    this.mousePoint = e.GetPosition(null);
}

private void ListBox_PreviewMouseMove(object sender, MouseEventArgs e)
{
    Point newPoint = e.GetPosition(null);
    Vector diff = this.mousePoint - newPoint;

    ListBox listBox = sender as ListBox;
    var listBoxItem = listBox.SelectedItem;

    if (e.LeftButton == MouseButtonState.Pressed)
    {
        ///Drag/Drop stuff here.
    }
}
2
  • please post your ItemTemplate here. May be the itemtemplate overlapped with scrollviewer, lets have a look at this too Commented Apr 13, 2015 at 9:56
  • The ItemTemplate currently does not deal with any drag/drop operations. All it does is filter items from a collection stored within my viewmodel. See answer here - link Commented Apr 13, 2015 at 12:24

1 Answer 1

3

The ScrollViewer is part of the ListBox, which is the sending object for ListBox_PreviewMouseMove. I think that making the ListBoxItem the sender, rather than the ListBox, should remove the ScrollViewer from that event.

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

5 Comments

Or he could check which Type e.OriginalSource is and act upon that, but your solution is probably better when it comes to performance.
To elaborate on this answer: You can attach the mouse events to each individual ListBoxItem instead of the whole ListBox by setting ListBox.ItemContainerStyle. See this answer to a similar question for an example: stackoverflow.com/questions/3235496/…
@goobering If I were to implement this, what would I use as my dragSource in my DoDragDrop method? Currently it's set to the listBox set in the ListBox_PreviewMouseMove method.
@Staeff I have tried this by just having an if() & else if() checking for a Thumb type and unselecting all items if the scrollbar is selected. Works for now.
Keep the ListBox as the drag source, but put the PreviewMouseMove event on the ListBoxItem.

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.