Reputation: 99
I have added a MapPolyline to my Bing Map. However, the map seems to be swallowing just the LeftMouseUp event. The event works properly for MouseDown and even RightMouseUp, but not for LeftMouseUp. For my design I have to use LeftMouseUp event and cannot use the others. Why isn't the event firing, and how can I fix it?
public MainWindow()
{
InitializeComponent();
var testLine = new MapPolyline();
var locations = new LocationCollection();
locations.Add(new Location(50, 50));
locations.Add(new Location(50, 60));
locations.Add(new Location(50, 70));
testLine.Locations = locations;
testLine.Stroke = Brushes.Black;
testLine.StrokeThickness = 15;
testLine.PreviewMouseDown += ExampleHandlerDown;
testLine.PreviewMouseUp += ExampleHandlerUp;
MainMap.Children.Add(testLine);
}
private void ExampleHandlerDown(object sender, MouseEventArgs e)
{
Console.WriteLine("Mouse Down");
}
private void ExampleHandlerUp(object sender, MouseEventArgs e)
{
Console.WriteLine("Mouse Up");
}
<Window x:Class="StackQuestion.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="1300">
<m:Map Name="MainMap" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CredentialsProvider="My Credentials"/>
</Window>
Running the code has the following results: - Left Mouse Click prints "Mouse Down" only - Right Mouse Click prints both "Mouse Down" and "Mouse Up" Left Mouse Click should match Right Mouse Click behavior.
Upvotes: 1
Views: 245
Reputation: 1424
you must set e.Handled = true;
in your ExampleHandlerDown
function first so that it knows to call MouseUp after a handled Down event
private void ExampleHandlerDown(object sender, MouseEventArgs e)
{
Console.WriteLine("Mouse Down");
e.Handled = true;
}
Why does this work?
Not handling the mouse down event (especially preview mouse down event) will cause the mouse down event to travel to the next layer which is the map; which means the map also receives a mouse down event. The map is doing something fishy when it gets a mouse down event preventing your mouse up from working.
Add these to see what I mean
MainMap.MouseDown += MainMap_MouseDown;
MainMap.MouseUp += MainMap_MouseUp;
private void MainMap_MouseUp(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Map Mouse Up");
}
private void MainMap_MouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Map Mouse Down <-- Something going on in here");
}
Comment out your e.Handled = true
and you'll see this in this output
Mouse Down Map Mouse Down <-- Something going on in here Map Mouse Up
Put your e.Handled = true
line back in and you'll see this output
Mouse Down Mouse Up Map Mouse Up
So you'll see any unhandled events are getting passed to the map. You should probably handle your mouse up event too to prevent the map doing any weird things you don't want too. I guess it doesn't do anything fishy when it's a right click. You'll need to see the source of bing maps to see what it's actually doing
Upvotes: 3
Reputation: 119
As you correctly stated, some component is blocking the event from handling where you want it to. This is because you are using a bubbling event (MouseDown). WPF offers a variant, PreviewMouseDown, which is a tunneling event. You can read more about the differences between the two here.
Basically what it comes down to is that bubbling events are handled in an order starting from the source element going up the visual tree through each parent element. Tunneling events are the opposite — they start at the element that's highest up in the visual tree and travel towards whatever fired it. This effectively means that whatever is blocking the event from reaching the handler you want will be bypassed when you use a tunneling event, because you're coming at it from a different direction.
Upvotes: 1