3

I draw SQUARE and other shapes in WPF. Like that,

 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;
    }

and i can delete random lines with that

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!!?
                }
            }
    }

I can delete random lines but i cant delete shapes

how can i?

Tayfun Yaşar
  • 422
  • 2
  • 9
  • 24

1 Answers1

2

You are using a foreach loop over the elements collection and changing the collection at the same time. That is not possible.

You could solve this by using a for-loop (be careful, the length of the loop might change due to the removal of elements)

Emond
  • 50,210
  • 11
  • 84
  • 115
  • I wanna delete selected random lines (myInkCanvas.Strokes.Erase(e.GetSelectedStrokes().GetBounds());) and defined shapes(ellipse, rectangle) myInkCanvas.Children.Remove..?? – Tayfun Yaşar Aug 07 '12 at 08:25
  • I don't really understand what you are trying to do and why it doesn't work. You can remove shapes from a canvas using Remove. – Emond Aug 07 '12 at 10:40