문제

단일 텍스트 상자로 표시되는 항목이 포함 된 목록 상자가 있습니다.

사용자가 버튼을 클릭하면이 모든 텍스트 상자를 통해 반복하고 바인딩 표현이 오류가 깨끗한 지 확인하고 싶습니다. 다음과 같아야합니다.

    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

모든 토론은 정말 감사 할 것입니다. 감사.

도움이 되었습니까?

해결책

더 쉬운 방법이있을 수 있지만 여기에 작동하는 옵션 중 하나가 있습니다.

1) 항목 목록을 반복하십시오.

항목 소스를 사용하고 있기 때문에 ListBox.Items 항목 소스의 데이터 항목을 참조합니다.

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 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) 마지막으로 텍스트 상자의 바인딩을 검사하십시오.

모두 다음과 같이 정리합니다.

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;
}

다른 팁

이전 게시물을 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 

엔드 기능

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top