当我想在画布中添加usercontrol时,我会出现此错误。 有一个组合框,当它的选择改变了,然后第一个通过这个方法删除网格中的所有usercotrol:
Dim childToDelete As New List(Of UIElement)
For Each a As UIElement In gridimgFloor.Children
childToDelete.Add(a)
Next
For Each c As UIElement In childToDelete
gridimgFloor.Children.Remove(c)
Next
之后想要添加新的相同usercontorls。但我得到了这个错误:
Specified element is already the logical child of another element. Disconnect it first.
我通过以下代码添加控件:
selectedFloorDevices = f.GetDevies
For Each d As DeviceIcon In selectedFloorDevices
Dim a As DeviceIcon = d
'Dim a As New DeviceIcon
Dim gr As New Canvas
a.Rectangle.Height = f.GetDeviceScale
a.Rectangle.Width = f.GetDeviceScale * 2
gr.Children.Add(a)
gridimgfloor.add(gr)
Next
它在这一行中出错:
gr.Children.Add(a)
问题是什么?直到最近3天我一直在挣扎,但一无所获。请帮忙。
答案 0 :(得分:0)
您正在使用以下内容:
For Each d As DeviceIcon In selectedFloorDevices
Dim a As DeviceIcon = d
您将a设置为等于d,请记住这些是引用类型,a不是d的副本,是 d。
以下一行是正确的:
Dim a As New DeviceIcon