我们知道,在将属性附加到画布上绘制的形状时,形状对象有标记属性非常方便。另一方面,我们鼓励使用轻量级绘图对象,例如:LineGeometry
如何将唯一属性附加到该类的实例?
注意:我想在画布上添加类似1000+的线条,我也希望能够识别哪一行是哪一行。因为这些线代表了像钢筋这样的结构元素的各个部分。所以我想要能够点击一条线,并识别它代表哪个钢筋。
发布于 2014-03-19 16:42:22
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);https://stackoverflow.com/questions/22512010
复制相似问题