简体   繁体   English

MouseDown for Ellipse不起作用

[英]MouseDown for Ellipse doen't work

I have a problem. 我有个问题。 I have an Ellipse: ellipse1 that is inside of a Canvas: canvas1. 我有一个Ellipse:ellipse1在Canvas:canvas1内部。 When I click on ellipse1, it's fill is changed from White to PaleVioletRed. 当我单击ellipse1时,其填充从白色更改为PaleVioletRed。 When I click on another part of canvas1 the ellipse will be moved there. 当我单击canvas1的另一部分时,椭圆将移动到那里。 This works. 这可行。

When I click again on ellipse1 its fill color doesn't change. 当我再次单击ellipse1时,其填充颜色不变。

What's wrong?... 怎么了?...

I have this XAML code for ellipse1: 我有此ellipse1的XAML代码:

<Ellipse Height="35" HorizontalAlignment="Left" Name="ellipse1" Stroke="Black" VerticalAlignment="Top" Width="70" Fill="White" StrokeThickness="3" Canvas.Left="71" Canvas.Top="70" MouseDown="pion_alb1_md"/>

This is C# part: 这是C#部分:

private void pion_alb1_md(object sender, EventArgs e)
{
    if (ellipse1.Fill == Brushes.White)
    {
        ellipse1.Fill = Brushes.PaleVioletRed;
    }
    else
    {
        ellipse1.Fill = Brushes.White;
    }
}

This is XAML code for canvas1: 这是canvas1的XAML代码:

<Canvas Name="piese_canvas" MouseDown="mouse_down_canvas_piese" Background="#43FCFFEB">
.......
</Canvas>

...and here is the C# part for canvas1: ...这是canvas1的C#部分:

private void mouse_down_canvas_piese(object sender, EventArgs e)
{
    if (ellipse1.Fill == Brushes.PaleVioletRed)
    {            
        Point c = Mouse.GetPosition(piese_canvas);

        if ((c.X > 81) && (c.Y < 311) && (c.X <160) && (c.Y >191))
        {
            Canvas.SetLeft(ellipse1, 72);
            Canvas.SetTop(ellipse1, 241);
            ellipse1.Fill = Brushes.White;
            Canvas.SetLeft(ellipse5, -12);
            Canvas.SetTop(ellipse5, 241);
        }
    }
}

Change the ellipse mouse down handle like below. 如下更改椭圆鼠标的向下手柄。 If you don't set e.Handled = true the mouse event will also be handled by the canvas and mouse_down_canvas_piese will be called immediately afterwards. 如果未设置e.Handled = true则鼠标事件也将由画布处理,之后将立即调用mouse_down_canvas_piese And since after moving the ellipse nearly completely lies in the "active area", Fill will be reset to White . 由于移动椭圆后几乎完全位于“活动区域”,因此“ Fill将重置为“ White

You could have found this out by debugging. 您可能通过调试发现了这一点。 Also note that your ellipse will never move to another position, since the new Top and Left values are hardcoded to 241 and 72. 还要注意,由于新的“ Top和“ Left值被硬编码为241和72,因此椭圆永远不会移动到另一个位置。

private void pion_alb1_md(object sender, RoutedEventArgs e)
{
    if (ellipse1.Fill == Brushes.White)
    {
        ellipse1.Fill = Brushes.PaleVioletRed;
    }
    else
    {
        ellipse1.Fill = Brushes.White;
    }
    e.Handled = true;
}

I've copied your code into a new project, and it works differently to your description. 我已经将您的代码复制到一个新项目中,它的工作方式与您的描述不同。

When I click on the ellipse, it changes colour. 当我单击椭圆时,它会更改颜色。 When I click on the canvas, the line if ((cX > 81) && (cY < 311) && (cX < 160) && (cY > 191)) returns false so the ellipse never moves. 当我在画布上单击时, if ((cX > 81) && (cY < 311) && (cX < 160) && (cY > 191))返回false,则椭圆永远不会移动。 This is probably because all I have on the window is a canvas holding an ellipse, and the canvas doesn't have a location set. 这可能是因为我在窗口上仅有的是一个容纳椭圆的画布,并且该画布未设置位置。

So I can't recreate your problem. 所以我无法重提您的问题。 Can you provide more information about what is in the canvas, or in the window? 您能否提供有关画布或窗口中内容的更多信息?

EDIT: 编辑:

OK, I see the problem now. 好的,我现在看到了问题。 Try adding breakpoints to both of your methods. 尝试将断点添加到这两种方法中。 Any clicks on the ellipse are also handled by the canvas (part of WPFs routed events). 椭圆上的任何单击也由画布处理(WPF路由事件的一部分)。 When the ellipse has moved, any clicks on it are inside the bounds of the canvas's special zone, and so it is changed to PaleVioletRed (because the ellipse was clicked on) and then immediately changed to White (because the canvas was clicked in the right place). 椭圆移动后,对其的任何单击都在画布的特殊区域的范围内,因此将其更改为PaleVioletRed(因为已单击椭圆),然后立即更改为白色(因为在右侧单击了画布)地点)。

Try changing your ellipse click handler to: 尝试将椭圆点击处理程序更改为:

private void pion_alb1_md(object sender, RoutedEventArgs e)
{
    if (ellipse1.Fill == Brushes.White)
    {
        ellipse1.Fill = Brushes.PaleVioletRed;
    }
    else
    {
        ellipse1.Fill = Brushes.White;
    }
    e.Handled = true;
}

The key points are: 关键点是:

  • EventArgs is now RoutedEventArgs. EventArgs现在是RoutedEventArgs。 This exposes the IsHandled property... 这暴露了IsHandled属性...
  • e.IsHandled = true. e.IsHandled = true。 This stops other event handlers (such as the canvas click handler) from firing. 这将阻止其他事件处理程序(例如画布单击处理程序)触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM