所以我有两个组成部分。第一个是代表节点的Image。然后为父图像的每个角落另外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个不同属性中的相同值)。 我确定有更优雅的方式这样做。
答案 0 :(得分:0)
您应该将成员对象添加到NodePlaceholder
类以指回&#34; Parent&#34;,然后确保分配它,例如:
foreach (var placeholder in PlaceholderList)
{
TargetCanvas.Children.Add(placeholder);
placeholder.Parent = this; //or this.ID, or whatever you need to access...
}