sunnysesh
sunnysesh

Reputation: 123

WPF - Listbox scrolling when an item is selected

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

Upvotes: 0

Views: 1596

Answers (1)

goobering
goobering

Reputation: 1553

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.

Upvotes: 3

Related Questions