0

所以我有两个作文课。第一个是代表节点的图像。然后为父图像的每个角另外 4 个图像,它们代表占位符。

public class Node : Image 
{
    public int ID { get; set; }
    public int Type { get; set; }
    public List<NodePlaceholder> PlaceholderList = new List<NodePlaceholder>();

    //creating Node Image, Creating 4 instances of NodePlaceholder for each corner and adding them to PlaceholderList

    TargetCanvas.Children.Add(this);
    foreach (var placeholder in PlaceholderList)
    {
         TargetCanvas.Children.Add(placeholder);
    }
 }


public class NodePlaceholder : Image
{
     this.MouseLeftButtonDown += NodePlaceholder_MouseLeftButtonDown;
}

void NodePlaceholder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    //how to get Parent Node ID?
}

你能告诉我如何访问父母ID吗?

我通过传递创建内部属性来表示父 ID 并将其传递给 NodePlaceholders 构造函数来完成此操作,但这会产生冗余(5 个不同属性中的值相同)。我相信有更优雅的方式可以做到这一点。

4

1 回答 1

0

您应该在NodePlaceholder类中添加一个成员对象以指向“父级”,然后确保分配它,例如:

foreach (var placeholder in PlaceholderList)
{
     TargetCanvas.Children.Add(placeholder);
     placeholder.Parent = this; //or this.ID, or whatever you need to access...
}
于 2013-10-31T13:51:05.887 回答