一旦我使用visualTreeHelper.getchild查找子对象,我如何获得该对象的名称,甚至是宽度或高度等对象的其他属性?
即
这不起作用:
For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(Can1) - 1
Dim ChildVisual As Visual = CType(VisualTreeHelper.GetChild(Can1, i), Visual)
Dim ChildName As DependencyProperty = childVisual.GetValue(Name)
它表示“Name”类型的值无法转换为system.windows.dependencyProperty
这也不起作用(但至少它编译):
For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(Can1) - 1
Dim childVisual As Visual = CType(VisualTreeHelper.GetChild(Can1, i), Visual)
Dim GT1 As GeneralTransform = childVisual.TransformToAncestor(Can1)
Dim currentpoint As Point = GT1.Transform(New Point(0, 0))
x = currentpoint.X
y = currentpoint.Y
如果我将鼠标悬停在childvisual上,我可以查看它的属性并看到该名称已设置为我在画布上的图像名称(Can1)。 但是,X和Y总是为0。
答案 0 :(得分:0)
我终于在网上找到了这个,看起来效果很好。
Dim childVisual As Visual = CType(VisualTreeHelper.GetChild(Can1, i), Visual)
Dim ChildName As String = ChildVisual.GetValue(Control.NameProperty)