我们知道Shape对象的Tag属性在将属性附加到画布上绘制的形状时非常方便。另一方面,我们鼓励我们使用轻量级绘图对象,例如:LineGeometry
如何将唯一属性附加到此类的实例?
注意: 我想在画布上添加1000多行,我也希望能够识别哪个是哪个。由于这些线将代表钢筋等结构元素的各个部分。所以我希望能够点击一条线并识别它代表的是哪个钢筋。
答案 0 :(得分:1)
LineGeometry
被声明为sealed
,这意味着您无法直接扩展它。但是,没有什么可以阻止您声明具有类型LineGeometry
的属性的新类以及其中的新属性:
public class ExtendedLineGeometry
{
public object CustomProperty { get; set; }
public LineGeometry LineGeometry { get; set; }
}
然后,无论您想要访问LineGeometry
对象,只需要像这样引用它:
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = extendedLineGeometry.LineGeometry;
根据您在额外属性中添加的内容,您可以将其定义为double
,甚至可以添加另一个并执行以下操作:
Path myPath = new Path();
myPath.Stroke = extendedLineGeometry.CustomProperty;
myPath.StrokeThickness = extendedLineGeometry.CustomProperty2;
myPath.Data = extendedLineGeometry.LineGeometry;
更新>>>
我以为我刚刚解释了你将如何使用它。但是,您的评论让我相信您并不理解。您的GeometryGroup.Children
属性可以使用LineGeometry
对象。您的ExtendedLineGeometry
对象中包含LineGeometry
个对象,因此您需要做的就是将其传递给Children
集合:
geometryGroup.Children.Add(extendedLineGeometry.LineGeometry);