C# 矩形是画布的子对象导致的问题

C# 矩形是画布的子对象导致的问题,c#,wpf,canvas,rectangles,C#,Wpf,Canvas,Rectangles,我有一个画布,周围有一个定义如下的边框 XAML: 这就是鼠标事件 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.

我有一个画布,周围有一个定义如下的边框

XAML:


这就是鼠标事件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace testing
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private double[][] array;
        private Rectangle rectCurrent;
        private int xPrevious = -1, yPrevious = -1, xCurrent = 0, yCurrent = 0;

        public MainWindow()
        {
            InitializeComponent();

            XDocument doc = XDocument.Load("Akulivik_Clay_High_Shrub.Xml");
            array = doc.Root.Elements("Month").Select(month => month.Elements().Select(x => (double)x).ToArray()).ToArray();
        }

        private void CanvaContainer_MouseMove(object sender, MouseEventArgs e)
        {
            Position(Mouse.GetPosition(CanvaContainer).X, Mouse.GetPosition(CanvaContainer).Y, out xCurrent, out yCurrent);

            valueXY.Content = String.Format("Array[Y : {1}][X : {0}] : {2}", xCurrent, yCurrent, array[yCurrent][xCurrent >= array.GetLength(0) ? 12 : xCurrent].ToString());

            realX.Content = "X : " + e.GetPosition(CanvaContainer).X;
            realY.Content = "Y : " + e.GetPosition(CanvaContainer).Y;

            if (!(xCurrent == xPrevious) && !(yCurrent == yPrevious))
                {

                    rectCurrent = new Rectangle();
                    rectCurrent.Stroke = new SolidColorBrush(Colors.Black);
                    rectCurrent.StrokeThickness = 2;
                    rectCurrent.Width = 45;
                    rectCurrent.Height = 40;
                    Canvas.SetLeft(rectCurrent, 45 * (xCurrent - 1));
                    Canvas.SetTop(rectCurrent, 40 * yCurrent);
                    CanvaContainer.Children.Add(rectCurrent);
                }
            else if (xCurrent == xPrevious && yCurrent == yPrevious)
                {
                    Console.WriteLine("Nothing");
                    xPrevious = xCurrent;
                    yPrevious = yCurrent;
                }
            else
                {
                    CanvaContainer.Children.Remove(rectCurrent);
                    Console.WriteLine("Outside");
                }       
        }

        private void Position(double xPos, double yPos, out int xValue, out int yValue)
        {
            xValue = (int)Math.Truncate(xPos / (int)45) +1;
            yValue = (int)Math.Truncate(yPos / (int)40);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Xml.Linq;
命名空间测试
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
专用双[]阵列;
私有矩形电流;
private int xpprevious=-1,ypprevious=-1,xCurrent=0,yCurrent=0;
公共主窗口()
{
初始化组件();
XDocument doc=XDocument.Load(“Akulivik_Clay_High_shuble.Xml”);
array=doc.Root.Elements(“月”).Select(月=>Month.Elements().Select(x=>(双精度)x.ToArray()).ToArray();
}
私有void CanvaContainer\u MouseMove(对象发送方,MouseEventArgs e)
{
位置(Mouse.GetPosition(CanvaContainer).X,Mouse.GetPosition(CanvaContainer).Y,out-xCurrent,out-yccurrent);
valueXY.Content=String.Format(“数组[Y:{1}][X:{0}]:{2}”,xCurrent,yCurrent,数组[yCurrent][xCurrent>=Array.GetLength(0)?12:xCurrent].ToString();
realX.Content=“X:+e.GetPosition(CanvaContainer).X;
realY.Content=“Y:+e.GetPosition(CanvaContainer).Y;
如果(!(xCurrent==xPrevious)和(!(yCurrent==yPrevious))
{
rectCurrent=新矩形();
rectCurrent.Stroke=新的SolidColorBrush(Colors.Black);
rectCurrent.StrokeThickness=2;
直流宽度=45;
当前高度=40;
SetLeft(rectCurrent,45*(xCurrent-1));
Canvas.SetTop(rectCurrent,40*yCurrent);
CanvaContainer.Children.Add(当前);
}
else if(xCurrent==xPrevious&&yCurrent==yPrevious)
{
Console.WriteLine(“无”);
xPrevious=xCurrent;
yPrevious=yCurrent;
}
其他的
{
CanvaContainer.Children.Remove(rectCurrent);
控制台。写入线(“外部”);
}       
}
专用空位(双XPO、双YPO、out int xValue、out int yValue)
{
xValue=(int)数学截断(xPos/(int)45)+1;
yValue=(int)数学截断(yPos/(int)40);
}
}
}
重点是,当我在鼠标上画一个矩形时,一切都很好,但是当我到达位于canva边界上的矩形的边界时,它会创建另一个矩形,因为该事件被引发,因为该矩形是canva的子对象,所以我假设它引发了相同的事件。行为如下图所示

到达边界:

到达时:


更清楚的是,在第二张图片上,即使我到达边界,也不应该有矩形。如果您需要更多描述,请在注释中说明。

编译和运行示例所需的大部分代码都缺失,如果您提供了一个完全有效的示例,那么您就有更好的机会回答这个问题。同时,请尝试将边框的SnapsToDevicePixels属性设置为“True”

更新:你在这里遇到了一些问题。画布大小错误,边距为1像素,边框没有足够的空间,并且假设命中测试意味着光标在画布限制内(WPF中的默认命中测试行为也是沿右边缘命中)。尝试将xaml更改为:

<Border x:Name="CanvasBorder" BorderThickness="1" BorderBrush="Black"  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="50,50,0,0" >
    <Canvas x:Name="CanvaContainer" Background="Transparent" Height="480" Width="540" MouseMove="CanvaContainer_MouseMove" />
</Border>

你不需要完整的代码,但是你做到了,它没有起作用,但是如果你让人们更容易,他们就更有可能重现你的问题。无论如何,我已经更新了我的答案。
<Border x:Name="CanvasBorder" BorderThickness="1" BorderBrush="Black"  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="50,50,0,0" >
    <Canvas x:Name="CanvaContainer" Background="Transparent" Height="480" Width="540" MouseMove="CanvaContainer_MouseMove" />
</Border>
private void CanvaContainer_MouseMove(object sender, MouseEventArgs e)
{
    var pt = Mouse.GetPosition(CanvaContainer);
    if ((pt.X >= CanvaContainer.ActualWidth) || (pt.Y >= CanvaContainer.ActualHeight))
        return;
    Position(pt.X, pt.Y, out xCurrent, out yCurrent);