简体   繁体   English

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

[英]Handling touch and mouse events in wpf

I have an application in which the listview items are being dragged and dropped. 我有一个将listview项拖放到其中的应用程序。 Its already done with Mouse Events. 它已经通过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. 我想将其更改为以最少的更改即可。.我在某处读了一篇文章,要求处理名为GlobalHandler的东西,并将其他事件与之关联,以便像我一样通用。 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? 在那种情况下,我如何检查事件是否为“向左按下”或“向左按下”等。还会与DragEnter事件一起工作吗? 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 这是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;
                    }
                }

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. InputEventArgs定义它的含义,然后object sender确定它的来源。

Check out this article 看看这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM