質問

私は、単一のテキストボックスで表されている項目が含まれているリストボックスを持っています。

ユーザーがボタンをクリックすると、

、私はすべてのこれらのテキストボックスを通して反復し、それらの結合式がエラーのクリーンであるかどうかを確認したいです。 以下のようなものである必要があります:

    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はのItemsSourceのデータ項目を参照します。

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

2)これらの項目のコンテナーを取得します。

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

3)使用VisualTreeHelperはビジュアルコンテナのテキストボックスの子を検索します。

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