Search code examples
c#wpfeventsmousetextblock

OnLeftButtonDown firing with difficulty after putting textblock with canvas.set


I have written a C# wpf program that deals with curves on a canvas. So I load a curve (a sequence of points in a polylinesegment) and then I operate various operations on it. Each curve is put on the screen through mouse interaction and that works fine. Each curve then comes with a textblock in its center which gives out some information. So the problem comes when I want to mouse-move the shape. I first select the shape with the mouse (that works) and then I stick it to the cursor through the OnMouseMove event. Eventually I put it down on the with the OnMouseLeftButtonDown event.

So in short the OnMouseLeftButtonDown always works fine except when I have to move the shape AND the label. In that case I have to press several times (randomly) to fire the event.

I have then searched the part which cause the problem and that is when I move the label.

    private void UpdateLabel(int index, PathInfo piToBeAdded)
    {
        plotCanvas.Children.Remove(names[index]);

        TextBlock text = new TextBlock();
        text.TextAlignment = TextAlignment.Left;
        text.FontSize = 12;
        text.Inlines.Add(new Run("(" + (GetPathsIndexFromId(piToBeAdded.ID) + 1) + ")ID:" + piToBeAdded.ID + " " + piToBeAdded.Name) { FontWeight = FontWeights.Bold });
        Canvas.SetLeft(text, piToBeAdded.Center.X);<-----those cause the problem
        Canvas.SetTop(text, piToBeAdded.Center.Y);<------those cause the problem
        text.ReleaseMouseCapture();
        names[index] = text;            
        plotCanvas.Children.Add(text);
    }

NB: pathinfo is just a class storing some information among which also coordinates Specifically it's just the Canvas.SetLeft and Canvas.SetTop that causes the OnMouseLeftButtonDown not to fire properly. I I take them off the label goes in 0,0 and the event But what's wrong with those instructions? How can I make the OnLeftButtonDownEvent to work properly?

I hope that I have described the problem properly I've tried to put all related information.

Thanx in advance

Patrick


Solution

  • Toadflakz thanx in any case I am not sure if my solution has got to do with what you suggest. In case I will give you the flag. I noticed that the problem is related with the fact that when moving shape+textblock the mouse point is EXACTLY on the center of the shape (I did that on purpose) and therefore EXACTLY on the textblock. Therefore when I click i don't click on the canvas but on the label. This is why the canvas is not firing. I suppose that the label is firing. So in short I just moved the label some pixels away and that did the trick!!