在wpf中处理触摸和鼠标事件

时间:2017-11-21 21:46:57

标签: c# wpf xaml

我有一个应用程序,其中拖放列表视图项。它已经完成了鼠标事件。我想用最小的变化来改变它。我在某个地方读了一篇文章,要求处理一些名为GlobalHandler的事情,并将其他事件与之关联,以便像我那样使用它的通用。 在这种情况下,我如何检查事件是左按钮还是左按钮等。还会触摸DragEnter事件吗?这是我目前的代码。请帮帮忙。

<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"/>

以下是cs文件中的代码

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

1 个答案:

答案 0 :(得分:0)

那里有很多东西。触摸事件与鼠标事件不同,但拖动事件对两者都应该相同。 InputEventArgs定义它是什么,object sender告诉它来自何处。

查看this article