0

我试图阻止 WPF Bing Maps 控件在用户拖动图钉时平移。我所做的是,当用户选择带有 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;
            }
        }

    } 
4

0 回答 0