无法在画布WPF中检索子计数 [英] cannot retrieve child count in canvas WPF
本文介绍了无法在画布WPF中检索子计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,在我的项目中,我创建了一个名为"MyCanvas"的自定义画布,该画布实现了拖放操作.
拖放效果很好,但是当我尝试使用"VisualTreeHelper.GetChildrenCount方法"对它的子元素进行计数时.
我收到值不能为空.参数名称:元素"的错误.
这是我的代码段
--------------------------
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
--------------------------
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]添加了代码块[/edit]
[edit]code block added[/edit]
推荐答案
问题很简单:c
为空.我看不到任何代码来初始化该成员.确保已正确初始化.
同样,您通常应使用逻辑数量,而不是视觉数量的子代.只需使用MyCanvas.Children.Count
.但首先,请确保您使用正确的实例(如果您的类不是null).
—SA
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 useMyCanvas.Children.Count
. But first, make sure you use proper instance if your class, not null.
—SA
这篇关于无法在画布WPF中检索子计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文