有没有办法迭代ListBox项模板?

时间:2009-03-29 00:15:34

标签: wpf data-binding listbox listboxitem listboxitems

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

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

    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

任何讨论都会非常感激。 感谢。

2 个答案:

答案 0 :(得分:6)

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

1)遍历项目列表。

由于您使用的是商品来源,ListBox.Items会引用ItemsSource中的数据项。

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

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

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

3)使用VisualTreeHelper搜索容器visual的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;
}

答案 1 :(得分: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 

结束功能