首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Button是TextBlock?

Button是TextBlock?
EN

Stack Overflow用户
提问于 2013-03-16 07:25:25
回答 1查看 50关注 0票数 0

我使用这个函数来递归遍历ListBoxItem中的所有控件,并且在DataTemplate中既有Button又有TextBlock。它总是选择按钮而不是TextBlock。有人能看到我的函数出了什么问题吗?

代码语言:javascript
复制
Private Function FindVisualChild(ByVal obj As DependencyObject) As TextBlock
 Dim result As TextBlock = Nothing
 For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1
  Dim child As DependencyObject = TryCast(VisualTreeHelper.GetChild(obj, i), DependencyObject)
  If Not child Is Nothing AndAlso TypeOf child Is DependencyObject Then
    If TypeOf child Is TextBlock Then
      Dim tbl As TextBlock = TryCast(child, TextBlock)
      If Not tbl Is Nothing Then result = tbl
    Else
      Dim tbl As TextBlock = FindVisualChild(child)
      If Not tbl Is Nothing Then result = tbl : Exit For
    End If
  End If
Next
Return result

End函数

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-16 07:52:09

看起来很简单,但它确实有效!不能解释为什么它会与这条线下面的类型混淆。

代码语言:javascript
复制
Private Function FindVisualChild(ByVal obj As DependencyObject) As TextBlock
Dim result As TextBlock = Nothing
For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1
  Dim child As DependencyObject = TryCast(VisualTreeHelper.GetChild(obj, i), DependencyObject)
  If TypeOf child Is Button Then Continue For 'fixes it
  If Not child Is Nothing AndAlso TypeOf child Is DependencyObject Then
    If TypeOf child Is TextBlock Then
      Dim tbl As TextBlock = TryCast(child, TextBlock)
      If Not tbl Is Nothing Then result = tbl
    Else
      Dim tbl As TextBlock = FindVisualChild(child)
      If Not tbl Is Nothing Then result = tbl : Exit For
    End If
  End If
Next
Return result

End函数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15443668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档