2012-08-07 82 views
3

我在WPF中繪製了SQUARE和其他形狀。這樣,在wpf中刪除矩形/元素

private Rect DrawSquare(Rect bounds, InkCanvas canvas) 
    { 
     Rectangle recta = new Rectangle(); 
     recta.Stroke = Brushes.Black; 
     //recta.Fill = Brushes.Blue; 
     recta.StrokeThickness = 4; 
     recta.Width = bounds.Width; 
     recta.Height = bounds.Height; 
     InkCanvas.SetLeft(recta, bounds.Left); 
     InkCanvas.SetRight(recta, bounds.Right); 
     InkCanvas.SetTop(recta, bounds.Top); 
     canvas.Children.Add(recta); 
     return bounds; 
    } 

,我可以刪除隨機線與

private void myInkCanvas_SelectionChanging(object sender, InkCanvasSelectionChangingEventArgs e) 
     { 
      else if (toolbarMode == ToolbarMode.Delete) 
      { 
       myInkCanvas.Strokes.Erase(e.GetSelectedStrokes().GetBounds()); //can delete random lines 
       ReadOnlyCollection<UIElement> elements = e.GetSelectedElements(); 
       foreach (UIElement element in elements) 
       { 
        /*String ShapeName = ((System.Windows.Media.Visual)(element)).ToString(); 
        if (ShapeName.Contains("Rectangle")) 
        { 
         Rectangle recta = (Rectangle)element; 
         recta.Fill = Brushes.Blue; 
        }*/ 
        myInkCanvas.Children.Remove(element); //cant delete shapes!!? 
       } 
      } 
    } 

我可以刪除隨機線,但我不能刪除形狀

我哪有?

回答

2

您正在使用foreach循環遍歷元素集合並同時更改集合。這是不可能的。

您可以通過使用一個for循環解決這個問題(注意,環路的長度可能會改變,由於消除元件)

+0

我想刪除所選 隨機線(myInkCanvas.Strokes.Erase(E .GetSelectedStrokes()。GetBounds());) 和定義的形狀(橢圓,矩形) myInkCanvas.Children.Remove .. ?? – tayfun 2012-08-07 08:25:10

+0

我真的不明白你想要做什麼,爲什麼它不工作。您可以使用刪除從畫布中刪除形狀。 – 2012-08-07 10:40:41