简体   繁体   English

帆布儿童识别

[英]Canvas child identification

I have a node class with Point property called Location. 我有一个具有Point属性的节点类,称为Location。 I create several object of this class and then I put them on the generic list. 我创建了此类的几个对象,然后将它们放在通用列表中。 In my app every node is represended by the Image (node location == image location). 在我的应用中,每个节点都由Image表示(节点位置==图像位置)。

To render images I iterate my nodelist and add images to Canvas on my WPF main window. 为了渲染图像,我迭代了节点列表,并将图像添加到WPF主窗口上的Canvas中。 Basically first item on my list is the first child of my Canvas. 基本上,清单上的第一项是Canvas的第一个孩子。

When I change location of my node then when rendering Image automatically changes aswell. 当我更改节点的位置时,渲染图像时也会自动更改。 But if I want to move Image using MouseMove event I have no idea how to acces specific element from my nodelist. 但是,如果我想使用MouseMove事件移动Image,我不知道如何从我的节点列表访问特定元素。 How do I know which element is which? 我怎么知道哪个是哪个元素?

Since you add Images to the Canvas in the same order as the nodes in your nodelist, you may simply get the Canvas child index and access the node by that index: 由于以与节点列表中节点相同的顺序将图像添加到Canvas中,因此您可以简单地获取Canvas子索引并通过该索引访问节点:

private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var element = canvas.InputHitTest(e.GetPosition(canvas)) as UIElement;

    if (element != null)
    {
        int index = canvas.Children.IndexOf(element);
        node hitNode = nodelist[index];
    }
}

Click/drag/hover with your mouse over a specific image and transform its (0,0) points against canvas. 用鼠标单击/拖动/悬停在特定图像上,然后将其(0,0)点转换为画布。

Point relativePoint = image.TransformToAncestor(myCanvas)
                              .Transform(new Point(0, 0));

Then run through your list and find the image with location same as relativePoint. 然后遍历您的列表,找到位置与relativePoint相同的图像。

Thats how you find the one which captured your mouse cursor. 这就是您找到捕获鼠标光标的方式的方法。

Also this is a nice post to read about transforming in wpf. 这也是阅读有关在wpf中进行转换的不错的文章。

http://msdn.microsoft.com/en-us/library/ms750596.aspx http://msdn.microsoft.com/en-us/library/ms750596.aspx

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

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