gpt4 book ai didi

wpf - 删除wpf中的矩形/元素

转载 作者:行者123 更新时间:2023-12-01 15:53:36 24 4
gpt4 key购买 nike

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

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

我该怎么办?

最佳答案

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

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

关于wpf - 删除wpf中的矩形/元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11841450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com