Search code examples
c#wpfbing

Call zoom function map, related with MouseWheel event in WPF


I have WPF app with Bing map and i stuck in one moment. Сlicking on the left mouse button creates a Pushpin. And if you want to change scroll of map with mouse wheel while the cursor is on the inserted Pushpin, nothing will happen. I understand, that my items on the map don't have calling events. But if with MouseUp event, I redefined my method (function: myMap_MouseMove, str: pin.MouseUp += Pin_MouseUp;)

Code(XAML):

    <m:Map x:Name="myMap" ZoomLevel="15" Mode="AerialWithLabels" MouseMove="myMap_MouseMove" MouseUp="myMap_MouseUp"  />

Code(C#):

    private void myMap_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (isDragging)
        {
            isDragging = false;
            return;
        }

        switch (e.ChangedButton)
        {
            case System.Windows.Input.MouseButton.Left:
                myMap_MouseLeftButtonDown(sender, e);
                break;

            case System.Windows.Input.MouseButton.Right:
                myMap_MouseRightButtonDown(sender, e);
                break;

            default:
                break;

        }

    }
private void myMap_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
    ...
    foreach (var viewportPoint in drawPolyPoints)
    {
        // Convert the mouse coordinates to a location on the map
        ControlTemplate template = (ControlTemplate)this.FindResource("CutomPushpinTemplate");
        Pushpin pin = new Pushpin();
        pin.Location = viewportPoint;
        //pin.Template = template;
        pin.Style = (Style)this.FindResource("LabelPushpinStyle");
        pin.MouseUp += Pin_MouseUp;  // It's OK
        pin.MouseWheel += myMap.MouseWheel;  // problem
        myMap.Children.Add(pin);
    }

    double area = CalculateArea(_polyLocations);
    this.Fields.Text = "Area: " + area;
}
    private void myMap_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {

        ...
        e.Handled = true;
        Point mousePosition = e.GetPosition(myMap);

        Location pinLocation = myMap.ViewportPointToLocation(mousePosition);
        ControlTemplate template = (ControlTemplate)this.FindResource("CutomPushpinTemplate");

        // The pushpin to add to the map.
        Pushpin pin = new Pushpin();
        pin.Location = pinLocation;
        pin.Style = (Style)this.FindResource("LabelPushpinStyle");

        // Adds the pushpin to the map
        myMap.Children.Add(pin);
        drawPolyPoints.Add(pin.Location);

        ...

    }

I have some problems with MouseWheel event.

How I can call Map MouseWheel event from child(PushPin or another) ??


Solution

  • To raise the MouseWheelEvent on the map, you can declare a new eventHandler which will forward the event to the map.MouseWheelEvent handler:

    private void RaiseMyMapMouseWheel(object sender, MouseWheelEventArgs e)
        {
            var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
            {
                RoutedEvent = UIElement.MouseWheelEvent,
                //You can change the sender for myMap if you wish
                Source = sender
            };
            myMap.RaiseEvent(eventArg);
        }
    

    and then you add the handler like this:

    pin.MouseWheel += this.RaiseMyMapMouseWheel;