I try to add two Canvas to a List<Canvas>, but I receive exception from the following code:
List<Canvas> cvList = new List<Canvas>();
Canvas cv = new Canvas();
cv.Width = 100;
cv.Height = 100;
cvList.Add(cv); // adding first Canvas to List<Canvas>
cvList.Add(cv); // adding the second Canvas to List<Canvas>
...
To elaborate more on the issue, each Canvas has to be distinct since each may Children different TextBox, Label and other UIElement. So I think the above code shouldn’t work. However though I cannot do this:
Canvas cv1 = new Canvas();
cv1.Width = 100;
Canvas cv2 = new Canvas();
cv2.Width = 250;
...
Or
Canvas[] cv = new Canvas[myInt];
I cannot do the above because the size of the List is determine at run time and I cannot assign a size to an Array or declare each array individually.
How to do this correctly? Yes, I’ve read the List on MSDN, but the site didn’t tell me how to do so. Thanks.
To elaborate on Joels answer, this is what you need to do:
Note that adding the same element twice to the same
List<Canvas>collection in this way is perfectly legal, however attempting to use the same element twice in a layout (as might happen depending on the way that this list is used) is not.