Run FindVisualChildren several times in WPF

0

I have a text file that contains 10 words, only one of them per line. My intention is to call each line, visualize the word in Textbox and extract each letter of the word and mix it with random letters, the idea is that the user looks at the word and forms it using each button with the corresponding letter, as well as You can see it in the figure.

Screenshot:

In this picture you can see, how the buttons are set in xaml without any specific name, and when the code is executed, the letters are assigned using the function FindVisualChildren but that only works for the first word of the file.

How can I rerun this function several times?

To locate the letters (those corresponding to the word, and the random ones) inside the buttons I use a function called findvisualChildren that is responsible for assigning each button the corresponding letter and visualize it as its content.

This is the code:

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}

The next step is to verify that the word that the user has formed when clicking on the button corresponds to the extracted word, so that when the whole word is formed, a OK should appear and show the next word in the word file, but when that happens I can not reassign the new letters, because I can not call the function that has already been executed.

I do not know how to make this function run n times, if when I put it within a while until it does not meet the condition the result is not seen, therefore I can only see the last word of the file.

    
asked by JCTM 26.03.2016 в 05:25
source

1 answer

0

I would recommend two things to you:

first : when starting the window (onload) call the findvisualchildren and save the list of buttons in a List as a global variable of the form. so you already have all the buttons in that list so that you only have to run the findvisualchildren method once and avoid the problem you have.

Second : also when starting (onload) load the text file so that you have it in memory (in a variable type List for example) and you do not have to be reading the file every time it is complete a sentence.

I hope it is of your help, greetings.

    
answered by 10.04.2016 в 08:44