3

我在 WPF 中绘制 SQUARE 和其他形状。像那样,

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

我可以删除随机线条但我不能删除形状

我怎样才能?

4

1 回答 1

2

您正在对元素集合使用foreach循环并同时更改集合。这是不可能的。

您可以通过使用 for 循环来解决这个问题(请注意,循环的长度可能会由于元素的删除而改变)

于 2012-08-07T07:55:56.233 回答