Q:

Tracking mouse clicks while displaying a floating line

Tried to find a good way to word that..  So, my code drops a waypoint on a nautical chart on double click.  After the first waypoint is dropped, I want to create a line between the waypoint and the mouse cursor position.  That appears to be working, but after the line is drawn, I'm not longer able to interact with the map or drop any other waypoints.  The double-click events are not fired as it appears the thread is constantly redrawing the line on mouse_move, which is an event on a StackPanel.
public async void _navigationChartPanel_MouseMove(object sender, MouseEventArgs e)
{
    foreach (var element in _panelChart.Children)
    {
        if (element is Line && ((Line)element).Name == "RangeBearingLine")
        {
            index = _panelChart.Children.IndexOf(element);
            break;
        }
    }

    if (index >= 0)
    {
        _panelChart.Children.RemoveAt(index);
    }

    var line = new Line
    {
        Name = "RangeBearingLine",
        StrokeThickness = 1,
        Stroke = System.Windows.Media.Brushes.Red,
        X1 = _parent._routePlanner.lastWaypointLoc.X,
        Y1 = _parent._routePlanner.lastWaypointLoc.Y,
        X2 = mouseClick.X,
        Y2 = mouseClick.Y
    };

    _panelChart.Children.Add(line); 

From what I understand, I need to display the line below the control where it's still visible but doesn't prevent me from interacting with the UI.  I'm no expert with WPF, but I can't think of another way to display the line that follows the mouse other than by firing the MouseMove event over and over again.  Is there a better way?

A:

One way to solve this is to set IsHitTesting to false on the child elements you create. An easier way is to intercept PreviewMouseDown instead, because that's a tunneling event and will thus get called for the parent control before the children:
<Canvas x:Name="theCanvas" Background="CornflowerBlue" PreviewMouseDown="Canvas_MouseDown" PreviewMouseMove="Canvas_MouseMove">
    <Rectangle Canvas.Left="100" Canvas.Top="100" Width="200" Height="200" Fill="Green" /> <!-- to show that it will also work when you click on other children -->
</Canvas>

And then in your main window class:
public partial class MainWindow : Window
{
    private Line CurrentLine;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
    {           
        var pos = e.GetPosition(theCanvas);
        if (e.ClickCount == 2)
        {
            this.CurrentLine = new Line
            {
                Name = "RangeBearingLine",
                StrokeThickness = 1,
                Stroke = System.Windows.Media.Brushes.Red,
                X1 = pos.X,
                Y1 = pos.Y,
                X2 = pos.X,
                Y2 = pos.Y
            };
            theCanvas.Children.Add(this.CurrentLine);
            e.Handled = true;
        }
        else if ((e.ClickCount == 1) && (this.CurrentLine != null))
        {
            this.CurrentLine.X2 = pos.X;
            this.CurrentLine.Y2 = pos.Y;
            this.CurrentLine = null;
            e.Handled = true;
        }
    }
    private void Canvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (this.CurrentLine == null)
            return;
        var pos = e.GetPosition(theCanvas);
        this.CurrentLine.X2 = pos.X;
        this.CurrentLine.Y2 = pos.Y;
        e.Handled = true;
    }

}

