拖动图钉时禁用平移

时间:2012-12-13 15:00:57

标签: wpf drag-and-drop bing-maps pushpin panning

我正在尝试阻止用户拖动图钉时平移WPF Bing地图控件。我所做的是当用户选择带有MouseLeftButtonDown的图钉时,接管来自地图ViewChangeStart,ViewChangeOnFrame的事件并将e.Handled属性设置为true。

我所期待的是,如果我将该属性设置为true,则会取消事件并禁用平移。但是地图仍然在平移。

我尝试的另一种方法是将属性SupportedManipulations设置为None。这两个选项都没有预期的结果。

以下是我用于DraggablePushpin

的代码
 public class DraggablePushpin : Pushpin
    {
        private bool isDragging = false;

        protected override void OnPreviewMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            var parentLayer = this.Parent as MapLayer;
            if (parentLayer != null)
            {
                Map parentMap = parentLayer.Tag as Map;
                if (parentMap != null)
                {
                    parentMap.ViewChangeStart += parentMap_ViewChangeStart;
                    parentMap.MouseLeftButtonUp += parentMap_MouseLeftButtonUp;
                    parentMap.MouseMove += parentMap_MouseMove;

                    parentMap.SupportedManipulations = System.Windows.Input.Manipulations.Manipulations2D.None;
                }
            }

            this.isDragging = true;

            base.OnPreviewMouseLeftButtonDown(e);
        }

        protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
        }

        void parentMap_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            var map = sender as Map;
            // Check if the user is currently dragging the Pushpin
            if (this.isDragging)
            {
                // If so, the Move the Pushpin to where the Mouse is.
                var mouseMapPosition = e.GetPosition(map);
                var mouseGeocode = map.ViewportPointToLocation(mouseMapPosition);
                this.Location = mouseGeocode;
            }
        }

        void parentMap_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            (sender as Map).SupportedManipulations = System.Windows.Input.Manipulations.Manipulations2D.All;
        }

        protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
        {
            var parentLayer = this.Parent as MapLayer;
            if (parentLayer != null)
            {
                Map parentMap = parentLayer.Tag as Map;
                if (parentMap != null)
                {
                    parentMap.SupportedManipulations = System.Windows.Input.Manipulations.Manipulations2D.All;
                }
            }
        }

        void parentMap_ViewChangeStart(object sender, MapEventArgs e)
        {
            if (this.isDragging)
            {
                e.Handled = true;
            }
        }

    } 

0 个答案:

没有答案