Click here to Skip to main content
16,005,316 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello, In my project I have created a customized canvas named "MyCanvas" that implements drag and dropping.
Drag and dropping works well, but when i tried to count its child element using "VisualTreeHelper.GetChildrenCount Method ".
I got an error of "Value cannot be null. Parameter name: element".

Here is my code snippet
--------------------------

VB
Dim c As MyCanvas     
Protected Overrides Sub OnPreviewMouseDown(ByVal e As MouseButtonEventArgs)
            MyBase.OnPreviewMouseDown(e)
            Dim m As Integer
            m = VisualTreeHelper.GetChildrenCount(c) - 1
            MessageBox.Show(m)  'to display the number of 				                
				'child elements on the canvas
End Sub


[edit]code block added[/edit]
Posted
Updated 16-Nov-12 23:06pm
v2

1 solution

The problem is simple: c is null. I don't see any code where you initialize this member. Make sure it is properly initialized.

Also, you usually should use logical, not visual number of children. Simply use MyCanvas.Children.Count. But first, make sure you use proper instance if your class, not null.

—SA
 
Share this answer
 
Comments
Shmuel Zang 18-Nov-12 1:33am    
5'ed.
Sergey Alexandrovich Kryukov 18-Nov-12 11:05am    
Thank you, Shmuel.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900