0

このコードを使用して、ポイントからオブジェクトを動的に作成しました。

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 ) を割り当てる解決策が見つかりません。

解決策は何ですか、またはこの問題に対する適切な回避策はありますか? 多分それをキャストまたは変換しますか?

4

1 に答える 1

0

私が見ているように、あなたは重要なステップをスキップしています。最初にオブジェクトを宣言しPath、イベントを与えてから、次のように挿入しますCanvas

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);
Path pgObject = new Path({ Stroke = brushColor, StrokeThickness = 3, Data = pg });
pgObject.MouseDown+=new MouseButtonEventHandler(poly_MouseDown);
canvas.Children.Add (pgObject);
于 2016-11-02T10:06:11.383 に答える