我需要一些幫助來使用我的代碼。 combineShape.Freeze()函數看起來不太好。在代碼中使用GeometryGroup創建組合形狀
在這種情況下,我有兩個單選按鈕,名爲rbtPoint和rbtLine。當我單擊rbtPoint並單擊畫布時,它將創建一個點,現在我必須在畫布中創建2個點(ellipse1,ellipse2),在此步驟中,我已經可以在畫布周圍拖動Point(橢圓1或橢圓2) 。然後當我點擊rbtLine然後點擊第一個點(ellipse1)它會將點的顏色從紅色變爲藍色,然後當我再次點擊第二個點(橢圓2)時,它也會將顏色變成紅色,並且它會創建從ellipse1到ellipse2的連接藍線。 之後,使用GeometryGroup()函數將直線和2個點合併爲1個形狀。我看到的代碼從這個鏈接:
http://stackoverflow.com/questions/16417950/combining-linegeometry-with-ellipsegeometry-in-code-not-xaml
這裏是我的代碼來創建功能:
private void myCanvas_MouseDown(object sender, MouseButtonEventArgs e) { mouseDown = true; initialPoint = e.GetPosition(sender as IInputElement); if (rbtPoint.IsChecked == true) { if (e.OriginalSource is Ellipse) { captured = true; shapePoint = e.GetPosition(sender as IInputElement); clickedEllipse = (Ellipse)e.OriginalSource; uielement = (UIElement)e.OriginalSource; } else { shapeEllipse = new Ellipse(); (shapeEllipse as Ellipse).Width = 30; (shapeEllipse as Ellipse).Height = 30; (shapeEllipse as Ellipse).StrokeThickness = 5; (shapeEllipse as Ellipse).Fill = Brushes.Red; Canvas.SetLeft(shapeEllipse, initialPoint.X); Canvas.SetTop(shapeEllipse, initialPoint.Y); myCanvas.Children.Add(shapeEllipse); } } }
private void myCanvas_MouseUp(object sender, MouseButtonEventArgs e) { Mouse.Capture(null); captured = false; mouseDown = false; if (rbtLine.IsChecked == true) { if (e.OriginalSource is Ellipse) { captured = true; initialPoint = e.GetPosition(sender as IInputElement); clickedEllipse = (Ellipse)e.OriginalSource; uielement = (UIElement)e.OriginalSource; poin = e.MouseDevice.GetPosition(uielement); clickedEllipse.Fill = Brushes.Blue; if (firstpoint.X != 0 && firstpoint.Y != 0) { lineEndPoint = initialPoint; } else { firstpoint = initialPoint; } } if (lineEndPoint.X != 0 && lineEndPoint.Y != 0) { Point p = e.GetPosition(sender as IInputElement); shapeLine = new Line(); (shapeLine as Line).X1 = firstpoint.X; (shapeLine as Line).Y1 = firstpoint.Y; (shapeLine as Line).X2 = lineEndPoint.X; (shapeLine as Line).Y2 = lineEndPoint.Y; (shapeLine as Line).Stroke = Brushes.Blue; (shapeLine as Line).StrokeThickness = 5; //(shape as Line).Fill = Brushes.Blue; //Canvas.SetLeft(shape, firstpoint.X); //Canvas.SetTop(shape, firstpoint.Y); myCanvas.Children.Add(shapeLine); lineCreated = true; var ellipse1 = new EllipseGeometry(firstpoint, firstpoint.X, firstpoint.Y); var ellipse2 = new EllipseGeometry(lineEndPoint, lineEndPoint.X, lineEndPoint.Y); var line = new LineGeometry(this.firstpoint, this.lineEndPoint); GeometryGroup combinedShape = new GeometryGroup(); combinedShape.Children.Add(ellipse1); combinedShape.Children.Add(ellipse2); combinedShape.Children.Add(line); combinedShape.Freeze(); } } }
後,我跑combinedShape.Freeze()函數,當我再次點擊一個點並拖動它,它只會移動點擊點,而沒有其他點和線跟隨我的鼠標移動。
如何解決這個問題?我發現許多相關的文章,但他們中的大多數人在XAML中執行代碼而不是代碼隱藏。或者還有其他解決方法嗎? 我需要建議,對於這個問題,
感謝你的幫助,代碼大師..
Fikri
您是否檢查了這篇文章? https://msdn.microsoft.com/en-us/library/ms750593%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 –