有没有办法在列表框项目模板中进行迭代?
我有一个列表框,其中包含由单个文本框表示的项目。
当用户单击按钮时,我想遍历所有这些文本框并检查它们的绑定表达式是否没有错误; 应该是这样的:
Dim errCount = 0
For Each item In MyListBox.ListBoxItems 'There is no such thing ListBoxItems which is actually what I am looking for.
Dim tb As TextBox = item '.........Dig in item to extract the textbox from the visual tree.
errCount += tb.GetBindingExpression(TextBox.TextProperty).HasError
Next
If errCount Then
'Errors found!
End If
任何讨论都会非常感激。 谢谢。
I have a list box that contains items that are represented by a single textbox.
When the user clicks a button, I want to iterate thru all these text boxes and check if their binding expressions are clean of errors;
Should be something like:
Dim errCount = 0
For Each item In MyListBox.ListBoxItems 'There is no such thing ListBoxItems which is actually what I am looking for.
Dim tb As TextBox = item '.........Dig in item to extract the textbox from the visual tree.
errCount += tb.GetBindingExpression(TextBox.TextProperty).HasError
Next
If errCount Then
'Errors found!
End If
Any discussion would be really appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能有一种更简单的方法可以做到这一点,但这里有一个可行的选项:
1)迭代项目列表。
由于您使用的是项目源,
ListBox.Items
将引用 ItemsSource 中的数据项目。2) 获取这些物品的容器。
3) 使用 VisualTreeHelper 搜索容器视觉对象的 TextBox 子级。
使用此函数搜索正确类型的可视子项:
4) 最后,检查 TextBox 上的绑定。
所有这些放在一起,就像这样:
There may be an easier way to do this, but here is one option that will work:
1) Iterate through the list of items.
Because you are using items source,
ListBox.Items
will refer to the data items in the ItemsSource.2) Get the containers for these items.
3) Use VisualTreeHelper to search for a TextBox child of the container visual.
Use this function to search for a visual child of the correct type:
4) Finally, examine the binding on the TextBox.
All put together, something like this:
将上一篇文章翻译成 VB:
1)
2)
3)
4)
End Function
Translation of previous post to VB:
1)
2)
3)
4)
End Function