このコードを使用して、ポイントからオブジェクトを動的に作成しました。
SolidColorBrush brushColor = (SolidColorBrush)new BrushConverter().ConvertFromString(_brushColor);
PathFigure figures = new PathFigure();
figures.StartPoint = points[0];
points.RemoveAt(0);
figures.Segments = new PathSegmentCollection(points.Select((p, i) => new LineSegment(p, i % 2 == 0)));
PathGeometry pg = new PathGeometry();
pg.Figures.Add(figures);
canvas.Children.Add(new Path { Stroke = brushColor, StrokeThickness = 3, Data = pg });
次に、このオブジェクトのイベント ハンドラーを追加します。オブジェクトがパスまたはポリライン タイプの場合は問題になりません。次のようにイベント ハンドラを追加するだけです。
poly.MouseDown += new MouseButtonEventHandler(poly_MouseDown);
void poly_MouseDown(object sender, MouseButtonEventArgs e)
{
//code
}
問題は、MouseDown イベント ハンドラーを受け入れない Figure と PathGeometry を使用する必要があることです。それらは System.Windows からのものであるためです。Mediaクラスと Path/Polyline は System.Windows からのものです。Figure オブジェクトに適切なイベント ハンドラ ( MouseDown ) を割り当てる解決策が見つかりません。
解決策は何ですか、またはこの問題に対する適切な回避策はありますか? 多分それをキャストまたは変換しますか?