Cant see controls inside User Control in the VisualTreeHelper

I have UserControl in wpf 4.0 which contains buttons , labels , textboxes etc.... I want to loop those controls and when I get a buuton , I want to take it's name and save it to my list . Basically , all I want to do is to create a Names_list of all my buttons in the UserControl.

I have a method that iterates all the controls and if it finds a button , it saves it's name -

  public void EnumVisual(Visual myVisual)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
        {
            // Retrieve child visual at specified index value.
            Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

            Button _button = childVisual as Button;
            if (_button != null)
            {
                Class_Button _newButtonClass = new Class_Button();
                if (_button.Name != null)
                {
                    _newButtonClass.ButtonName = _button.Name; 
                }
                ButtonsList.Add(_newButtonClass);
            }

            // Enumerate children of the child visual object.
            EnumVisual(childVisual);

        }
    }

I always get an empty list. When I enter in to the code by debugging it and I watch the VisualTree of my UserControl , I see all the Panels and GroupBoxes and Grids but I dont see buttons , labels and texboxes although every control has a x:Name and every control is x:FieldModifier="public". This is very odd....And I cant understand the reason for that as well as how to solve this problem... can anyone tell what I am doing wrong? thanks

Answers


As suggested by @GazTheDestroyer you want to make sure the control template has been applied before trying to use VisualTreeHelper. Try:

public void EnumVisual(Visual myVisual)
{
    if(myVisual is FrameworkElement)
        ((FrameworkElement)myVisual).ApplyTemplate();

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
    {
        // Retrieve child visual at specified index value.
        Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

        Button _button = childVisual as Button;
        if (_button != null)
        {
            Class_Button _newButtonClass = new Class_Button();
            if (_button.Name != null)
            {
                _newButtonClass.ButtonName = _button.Name; 
            }
            ButtonsList.Add(_newButtonClass);
        }

        // Enumerate children of the child visual object.
        EnumVisual(childVisual);

    }
}

You can use a tool like Snoop or WPF Inspector to examine the visual tree of your control. If these tools are able to do so, the error must be somewhere in your code, right?


Need Your Help

MongoDb: How to drop a database using shell using authentication?

mongodb mongo-shell

I have a authenticated user with all required privileges to drop a database. I want to drop the database from the shell and tried following command

Whenever cron doesn't trigger backup

ruby-on-rails ruby whenever

I'm trying to integrate automatic backup mechanism to Rails app and decided to use backup gem. Backup file is valid and runs successfully manually, but when i deployed it on VPS it didn't run by cr...