1

我有一个列表框,其中包含由单个文本框表示的项目。

当用户单击一个按钮时,我想遍历所有这些文本框并检查它们的绑定表达式是否没有错误;应该是这样的:

    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

任何讨论将不胜感激。谢谢。

4

2 回答 2

6

可能有一种更简单的方法可以做到这一点,但这里有一个可行的选择:

1) 遍历项目列表。

因为你使用的是items source,所以ListBox.Items会引用ItemsSource中的数据item。

for (int i = 0; i < ListBox.Items.Count; i++)
{
    // do work as follows below...
}

2) 获取这些物品的容器。

DependencyObject obj = ListBox.ItemContainerGenerator.ContainerFromIndex(i);

3) 使用 VisualTreeHelper 搜索容器视觉对象的 TextBox 子项。

TextBox box = FindVisualChild<TextBox>(obj);

使用此函数搜索正确类型的可视子项:

public static childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
{
    // Search immediate children
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);

        if (child is childItem)
            return (childItem)child;

        else
        {
            childItem childOfChild = FindVisualChild<childItem>(child);

            if (childOfChild != null)
                return childOfChild;
        }
    }

    return null;
}

4) 最后,检查 TextBox 上的绑定。

全部放在一起,是这样的:

private bool ValidateList(ListBox lb)
{
    for (int i = 0; i < lb.Items.Count; i++)
    {
        DependencyObject obj = lb.ItemContainerGenerator.ContainerFromIndex(i);
        TextBox box = FindVisualChild<TextBox>(obj);
        if (!TestBinding(box))
            return false;
    }

    return true;
}
于 2009-04-07T11:49:35.703 回答
2

将上一篇文章翻译成 VB:

1)

For i As Integer = 0 To ListBox.Items.Count - 1 
    ' do work as follows below... 
Next

2)

Dim obj As DependencyObject = ListBox.ItemContainerGenerator.ContainerFromIndex(i)

3)

Dim box As TextBox = FindVisualChild(Of TextBox)(obj)
'************************
Public Shared Function FindVisualChild(Of ChildItem As DependencyObject)(ByVal obj As DependencyObject) As ChildItem
    ' Search immediate children 
    For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1
        Dim child As DependencyObject = VisualTreeHelper.GetChild(obj, i)
        If TypeOf child Is ChildItem Then
            Return child
        Else
            Dim childOfChild As ChildItem = FindVisualChild(Of ChildItem)(child)
            If childOfChild IsNot Nothing Then Return childOfChild
        End If
    Next
    Return Nothing
End Function

4)

Private Function ValidateList(ByVal lb As ListBox) As Boolean 
For i As Integer = 0 To lb.Items.Count - 1 
    Dim obj As DependencyObject = lb.ItemContainerGenerator.ContainerFromIndex(i) 
    Dim box As TextBox = FindVisualChild(Of TextBox)(obj) 
    If Not TestBinding(box) Then 
        Return False 
    End If 
Next 
Return True 

结束功能

于 2009-04-11T23:03:07.600 回答