Domanda

Ho una casella di riepilogo che contiene gli elementi che sono rappresentati da un unico testo.

Quando l'utente fa clic su un pulsante, voglio iterare attraverso tutte queste caselle di testo e verificare se le loro espressioni di legame sono puliti di errori; Dovrebbe essere qualcosa di simile:

    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

Qualsiasi discussione sarebbe molto apprezzato. Grazie.

È stato utile?

Soluzione

Ci può essere un modo più semplice per fare questo, ma qui è un'opzione che funziona:

1) Scorrere l'elenco degli elementi.

Poiché si utilizza elementi di origine, ListBox.Items farà riferimento alle voci di dati nel ItemsSource.

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

2) I contenitori per questi articoli.

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

3) Utilizzare VisualTreeHelper per la ricerca di un bambino TextBox del contenitore visivo.

TextBox box = FindVisualChild<TextBox>(obj);

Con questa funzione per la ricerca di un bambino visiva del tipo corretto:

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) Infine, esamina il legame sul TextBox.

Tutti messi insieme, qualcosa di simile a questo:

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

Altri suggerimenti

Traduzione di post precedente a 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 

End Function

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top