1

I'm trying to create a program that connects two objects. When MouseDown on the first object, I remember it (without releasing the mouse button), when I MouseUp on the second object, I want to create a connection between them, but when MouseUp on the second object, this event is triggered on the first object.

private void myThumb1_MouseDown(object sender, MouseButtonEventArgs e)
{
    inherObj1 = mt;
    connector = new Line
    {
        X1 = e.GetPosition(myCanvas).X,
        Y1 = e.GetPosition(myCanvas).Y,
        X2 = e.GetPosition(myCanvas).X,
        Y2 = e.GetPosition(myCanvas).Y,
        Stroke=Brushes.Black
    };
    myCanvas.Children.Add(connector);
}

private void myThumb1_MouseUp(object sender, MouseButtonEventArgs e)
{
    MyThumb mt = sender as MyThumb;
    if(isInher)
    {
        inherObj2 = mt;
        CreateInher(inherObj1, inherObj2);
    }
    e.Handled = true;
}

As I understand it, the MouseUp is attached to the object on which MouseDown worked. Is there any way around this?

techvice
  • 1,315
  • 1
  • 12
  • 24
D. Malakhov
  • 165
  • 1
  • 11
  • 1
    Try [hit testing on mouse up to find the element under the point where the event took place](https://stackoverflow.com/questions/45813/wpf-get-elements-under-mouse). A less respectable option might be to repurpose drag and drop. The drop event would be called on the target element. You'd want to replace the mouse cursor of course. You might end up fighting the framework with that one though. I'd go for hit testing. – 15ee8f99-57ff-4f92-890c-b56153 Jan 05 '18 at 14:58
  • I would use the MouseEnter event to keep record of the last object entered and then check it when I catch the MouseUp event – Francesca Aldrovandi Jan 05 '18 at 15:17

1 Answers1

0

Maybe you can make use of routed events? If both controls are in the same parent control, you could handle events for the child controls in the parent control, with something like this:

Xaml:

<Border Height="50" Width="300" BorderBrush="Gray" BorderThickness="1">
  <StackPanel Background="LightGray" Orientation="Horizontal" Button.Click="CommonClickHandler">
    <Button Name="YesButton" Width="Auto" >Yes</Button>
    <Button Name="NoButton" Width="Auto" >No</Button>
    <Button Name="CancelButton" Width="Auto" >Cancel</Button>
  </StackPanel>
</Border>

This only handles the Button.Click event, but should be possible for MouseDown/MouseUp.