0

I have an application in which the listview items are being dragged and dropped. Its already done with Mouse Events. I want to change it to touch with minimal changes.. I read a post somewhere asking to handle something called GlobalHandler and associate other events with that so that its generic like I did. In that case how can I check if the event is Left button down or left button up etc. Also will touch work with DragEnter event? Here is my code at the moment. Please help.,

<ListView
            Name="RunSetupListView"
            Grid.Row="0"
            Grid.Column="0"
            Grid.ColumnSpan="3"
            Style="{StaticResource RunSetupDisplayListViewStyle}"
            ItemsSource="{Binding RunSetupInfoList}"
            AlternationCount="100"
            LayoutUpdated="RunSetupListView_LayoutUpdated"
            PreviewMouseLeftButtonDown="GlobalHandler"
            PreviewMouseLeftButtonUp="GlobalHandler"
            MouseMove="GlobalHandler"
            DragEnter="RunSetupListView_DragEnter"
            DragOver="RunSetupListView_DragOver"
            Drop="RunSetupListView_Drop" 
            DragLeave="RunSetupListView_DragLeave"/>

Here is the code in the cs file

  private void GlobalHandler(object sender, InputEventArgs e)
            {
            //How to check if the event triggered from Touchup, touch down etc. Please help.
if(e.RoutedEvent==Mouse.MouseDownEvent)||//How can I add if its from touch here?    
            }
    private void RunSetupListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                // Do not even allow a drag/drop event to start if the mouse is pressed
                // on an area that is a tree view item
                var listViewItem =
                            VisualTreeHelperUtils.FindParent<ListViewItem>((DependencyObject)e.OriginalSource);
                if (listViewItem != null)
                {
                    SelectedDragItem = listViewItem;
                    // Log start point
                    StartPoint = e.GetPosition(null);
                }
                e.Handled = true;
            }

            private void RunSetupListView_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                // Reest in case only a click occurs in the tree view WITHOUT a drag event
                StartPoint = null;
                SelectedDragItem = null;
                e.Handled = true;
            }

            private void RunSetupListView_MouseMove(object sender, MouseEventArgs e)
            {
                if (!(sender is ListView))
                    return;

                // only for left button down and if we received a mouse down
                // event in the listview... sometimes this event will still get processed
                // even if you click outside the listview but then drag the mouse into the listview
                if (e.LeftButton == MouseButtonState.Pressed && StartPoint != null && SelectedDragItem != null)
                {
                    var mousePos = e.GetPosition(null);
                    var diff = StartPoint.Value - mousePos;

                    // Once the drag has been dragged far enough... prevents a drag from happening
                    // from simply clicking the item
                    if ((Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance
                        || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) &&
                        !_inDragMode)
                    {
                        var listView = sender as ListView;

                        var listViewItem =
                            VisualTreeHelperUtils.FindParent<ListViewItem>((DependencyObject)e.OriginalSource);

                        if (listViewItem == null)
                        {
                            return;
                        }

                        m_AdornerLayer = InitializeAdornerLayer(listViewItem, listView);
                        UpdateDragAdornerLocation(e.GetPosition(listView));
                        mousePoint = mousePos;
                        _inDragMode = true;
                        DataObject dragData = new DataObject(listViewItem.Content as DNA2RunInfoDisplay);
                        listView.CaptureMouse();
                        DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Move);

                        _inDragMode = false;
                        // Reset this here... the mouse up event DOES NOT get raised if we do a drag/drop effect
                        StartPoint = null;
                        SelectedDragItem = null;
                    }
                }
nikhil
  • 1,578
  • 3
  • 23
  • 52
  • Update: I said if(e.RoutedEvent==Mouse.MouseDownEvent). How can I also compare touch event along the same line? – nikhil Nov 21 '17 at 21:54

1 Answers1

0

There's a lot out there for this. Touch events are different than mouse events, but drag events should still work the same for either. The InputEventArgs define what it is, and the object sender tell where it's coming from.

Check out this article

wimpSquad
  • 31
  • 3
  • Thanks ultimately the solution here https://stackoverflow.com/questions/37285335/why-my-mouse-event-handlers-are-not-working-when-i-have-touch-event-handlers looks promising. Let me try it. – nikhil Nov 21 '17 at 22:04