热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

有方法在列表框项模板中迭代吗?-IsthereawaytoiterateinaListBoxItemstemplates?

Ihavealistboxthatcontainsitemsthatarerepresentedbyasingletextbox.我有一个列表框,其中包含由单个文本框表示

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.

如有任何讨论,我们将不胜感激。谢谢。

2 个解决方案

#1


6  

There may be an easier way to do this, but here is one option that will work:

也许有一种更简单的方法,但这里有一个方法:

1) Iterate through the list of items.

1)遍历项目列表。

Because you are using items source, ListBox.Items will refer to the data items in the ItemsSource.

因为您正在使用项目源列表框。项将引用ItemsSource中的数据项。

for (int i = 0; i 

2) Get the containers for these items.

2)拿到这些物品的容器。

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

3) Use VisualTreeHelper to search for a TextBox child of the container visual.

3)使用VisualTreeHelper搜索容器visual的TextBox子窗口。

TextBox box = FindVisualChild(obj);

Use this function to search for a visual child of the correct type:

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

public static childItem FindVisualChild(DependencyObject obj)
    where childItem : DependencyObject
{
    // Search immediate children
    for (int i = 0; i (child);

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

    return null;
}

4) Finally, examine the binding on the TextBox.

最后,检查文本框上的绑定。

All put together, something like this:

把它们放在一起,就像这样:

private bool ValidateList(ListBox lb)
{
    for (int i = 0; i (obj);
        if (!TestBinding(box))
            return false;
    }

    return true;
}

#2


2  

Translation of previous post to VB:

前一篇微博到VB的翻译:

1)

1)

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

2)

2)

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

3)

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)

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 

End Function

结束函数


推荐阅读
author-avatar
laerla君君
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有