在使用canvas.set放置文本块后,OnLeftButtonDown难以触发 [英] OnLeftButtonDown firing with difficulty after putting textblock with canvas.set

查看:141
本文介绍了在使用canvas.set放置文本块后,OnLeftButtonDown难以触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个C#wpf程序来处理画布上的曲线。所以我加载了一条曲线(多段中的一系列点),然后我对它进行了各种操作。通过鼠标交互将每条曲线放在屏幕上,效果很好。然后每条曲线在其中心附带一个文本块,提供一些信息。所以当我想要鼠标移动形状时问题就出现了。我首先用鼠标选择形状(有效),然后通过OnMouseMove事件将其粘贴到光标上。最后我把它放在OnMouseLeftButtonDown事件上。



因此,简而言之OnMouseLeftButtonDown总是正常工作,除非我必须移动形状和标签。在这种情况下,我必须按几次(随机)来发射事件。



然后我搜索导致问题的部分,那就是我移动时标签。



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

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。胆大 });
Canvas.SetLeft(text,piToBeAdded.Center.X);< -----导致问题的原因
Canvas.SetTop(text,piToBeAdded.Center.Y);< --- ---那些导致问题
text.ReleaseMouseCapture();
names [index] = text;
plotCanvas.Children.Add(text);
}





该方法是从OnMouseMove调用的,它处理移动形状和标签后面的光标。这样可行。当我把它放在OnMouseLeftButtonDown上时,这个事件没有被触发。



注意:pathinfo只是一个存储一些信息的类,其中也包括坐标具体来说它只是Canvas.SetLeft和Canvas.SetTop导致OnMouseLeftButtonDown无法正常触发。我把它们从标签上取下来进入0,0和事件但这些指令有什么问题?如何让OnLeftButtonDownEvent正常工作?



我希望我已经正确地描述了问题我已经尝试了所有相关信息。



提前付款



Patrick

解决方案



Drag&在WPF中删除 [ ^ ]功能。

我注意到这个问题与以下事实有关:当移动形状+文本块时,鼠标点完全位于形状的中心(我故意这样做),因此在文本块上是完全正确的。因此,当我点击我不点击画布但在标签上。这就是画布没有射击的原因。我想这个标签正在开火。所以简而言之,我只是将标签移动了一些像素,这就是诀窍!!


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

解决方案

There is
Drag & Drop[^] functionality in WPF.


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!!


这篇关于在使用canvas.set放置文本块后,OnLeftButtonDown难以触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆