Andy
Andy

Reputation: 129

Wpf listview scroll does not work properly in drag and drop scenario

I have the following Xaml code

    <ListView x:Name="ListView" ItemsSource="{Binding Values}" MaxHeight="400" MaxWidth="500" MinWidth="160"
              VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"  ScrollViewer.IsDeferredScrollingEnabled="True"
              MouseMove="ListView_MouseMove">
        <ListView.View>
            <GridView x:Name="GridView" ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
                <!--<GridViewColumn Header="ID" DisplayMemberBinding="{Binding [0]}"  CellTemplate="{StaticResource ColumnCellTemplate}" />
                <GridViewColumn Header="Test" DisplayMemberBinding="{Binding [1]}" CellTemplate="{StaticResource ColumnCellTemplate}"/>-->
            </GridView>
        </ListView.View>
    </ListView>

and the relevant code behind for my issue as follows

 Private Sub ListView_MouseMove(sender As Object, e As MouseEventArgs)
        MyBase.OnMouseMove(e)
        Dim viewModel As QuestionAnswerViewModel = CType(DataContext, QuestionAnswerViewModel)
       'Dim vis As Visual = e.OriginalSource()
        If e.LeftButton = MouseButtonState.Pressed And viewModel IsNot Nothing AndAlso viewModel.Value IsNot Nothing Then
            Dim data As New DataObject
            data.SetData(DataFormats.StringFormat, viewModel.Value)

            'Inititate the drag-and-drop operation.
            DragDrop.DoDragDrop(Me, data, DragDropEffects.Copy Or DragDropEffects.Move)
        End If
    End Sub

Now the problem with my code is that when I scroll the MouseMove event gets triggered and then, of course the drag and drop method. I wanted to ask how can I properly use the e.OriginalSource() method in order to see if the user is dragging or not the scroll-bar.

Thank you

Upvotes: 1

Views: 720

Answers (1)

Andy
Andy

Reputation: 129

In order to get the the scroll working properly I have just added a new check in the If statement Not e.OriginalSource().GetType().Equals(GetType(Thumb))

The full code looks like this now

  Private Sub ListView_MouseMove(sender As Object, e As MouseEventArgs)
        MyBase.OnMouseMove(e)
        Dim viewModel As QuestionAnswerViewModel = CType(DataContext, QuestionAnswerViewModel)

        If e.LeftButton = MouseButtonState.Pressed And viewModel IsNot Nothing AndAlso viewModel.Value IsNot Nothing _
                AndAlso Not e.OriginalSource().GetType().Equals(GetType(Thumb)) Then
            Dim data As New DataObject
            data.SetData(DataFormats.StringFormat, viewModel.Value)

            DragDrop.DoDragDrop(Me, data, DragDropEffects.Copy Or DragDropEffects.Move)
        End If
    End Sub

Now whenever I drag the scroll bar the code will not go inside the If statement, therefore the scroll bar will work properly.

Upvotes: 1

Related Questions