事件处理程序部分工作
我在 WPF 应用程序的类中创建了两个相同的圆圈,并且我想为它们分配相同的事件。然而,只有其中一个事件处理程序可以工作,而另一个则不能。我尝试使用两个不同的名称实现相同的事件处理程序,但仍然发生相同的情况。以下代码可能存在什么潜在问题?
SmallCircle leftCircle1 = new InputCircle(Brushes.Yellow, 7, 7);
Ellipse s1Ellipse = leftCircle1.thisEllipse;
rectCanvas.Children.Add(s1Ellipse);
SmallCircle leftCircle2 = new InputCircle(Brushes.Yellow, 7, 7);
Ellipse s2Ellipse = leftCircle2.thisEllipse;
rectCanvas.Children.Add(s2Ellipse);
s1Ellipse.MouseLeftButtonDown += new MouseButtonEventHandler(input_MouseLeftButtonDown1);
s2Ellipse.MouseLeftButtonDown += new MouseButtonEventHandler(input_MouseLeftButtonDown1);
s3Ellipse.MouseLeftButtonDown += new MouseButtonEventHandler(output_MouseLeftButtonDown1);
private void output_MouseLeftButtonDown1(object sender, MouseButtonEventArgs e)
{
rectCanvas.MouseLeftButtonDown -= Canvas_MouseLeftButtonDown;
rectCanvas.MouseLeftButtonUp -= Canvas_MouseLeftButtonUp;
rectCanvas.MouseMove -= Canvas_MouseMove;
nodeComb.lineCreated = true;
Point StartPosition = e.GetPosition(sender as UIElement);
nodeComb.initialPoint = StartPosition;
}
private void input_MouseLeftButtonDown1(object sender, MouseButtonEventArgs e)
{
if (nodeComb.lineCreated == true)
{
Point EndPosition = e.GetPosition(sender as UIElement);
nodeComb.endingPoint = EndPosition;
nodeComb.createLine();
nodeComb.lineCreated = false;
}
}
特别是,s2Ellipse 事件处理程序可以工作,而 s1Ellipse 则不能。
只有底部输入圆 (s2) 监听处理程序,上面的不监听。 真的非常奇怪!
I create in a class of a WPF application two identical circles and I want to assign the same event to both of them. However only on one of them the event hander works while on the other not. I tried to implement the same event handler with two different names but still the same happens. What could a potential problem in respect of the following code?
SmallCircle leftCircle1 = new InputCircle(Brushes.Yellow, 7, 7);
Ellipse s1Ellipse = leftCircle1.thisEllipse;
rectCanvas.Children.Add(s1Ellipse);
SmallCircle leftCircle2 = new InputCircle(Brushes.Yellow, 7, 7);
Ellipse s2Ellipse = leftCircle2.thisEllipse;
rectCanvas.Children.Add(s2Ellipse);
s1Ellipse.MouseLeftButtonDown += new MouseButtonEventHandler(input_MouseLeftButtonDown1);
s2Ellipse.MouseLeftButtonDown += new MouseButtonEventHandler(input_MouseLeftButtonDown1);
s3Ellipse.MouseLeftButtonDown += new MouseButtonEventHandler(output_MouseLeftButtonDown1);
private void output_MouseLeftButtonDown1(object sender, MouseButtonEventArgs e)
{
rectCanvas.MouseLeftButtonDown -= Canvas_MouseLeftButtonDown;
rectCanvas.MouseLeftButtonUp -= Canvas_MouseLeftButtonUp;
rectCanvas.MouseMove -= Canvas_MouseMove;
nodeComb.lineCreated = true;
Point StartPosition = e.GetPosition(sender as UIElement);
nodeComb.initialPoint = StartPosition;
}
private void input_MouseLeftButtonDown1(object sender, MouseButtonEventArgs e)
{
if (nodeComb.lineCreated == true)
{
Point EndPosition = e.GetPosition(sender as UIElement);
nodeComb.endingPoint = EndPosition;
nodeComb.createLine();
nodeComb.lineCreated = false;
}
}
In particular, s2Ellipse event handler works, while s1Ellipse does not.
Only the bottom input circle (s2) listens to handler, the above does not.
Really very strange!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我终于解决了这个问题。我在矩形的下半部分添加一个文本框,其中包含每个矩形的名称。它有足够的宽度来覆盖小输入圆,因此鼠标事件处理程序不会应用于上面的圆。我只是修剪了它的宽度,处理程序适用于两个圆圈。真是骗人的...
Ok, I lost finally the problem. I add a textbox on the bottom half of the rectangle which contains the name of each rectangle. This had an adequate width to cover the small input circle, so as the mouse event handler not to apply to the upper circle. I just trimmed its width and the handler applies to both circles. Really deceptive...