Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
   }


That method is called from the OnMouseMove which deals with moving the shape AND the label following the cursor. That works. It's when I put it down with the OnMouseLeftButtonDown that this event is not firing.

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
Posted

There is
Drag & Drop[^] functionality in WPF.
 
Share this answer
 
Comments
Patrick70__ 13-Oct-15 5:45am    
I am sorry but I don't understand what's that got to do with my problem?
Sinisa Hajnal 13-Oct-15 10:25am    
You described the problem when you have to move some objects dragging and dropping them. Instead of trying to re-create functionality with various onClick events, why not try through built-in functionality.
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!!
 
Share this answer
 
Comments
Sinisa Hajnal 13-Oct-15 10:26am    
What if someone else needs to do it? Or if you miss the center and click somewhere else? Wouldn't it be better to bind label events to same handlers as for canvas and move both at the same time?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900