Shaboboo
Shaboboo

Reputation: 1579

Wpf: Listing all control Names in an application

I need a way to list out all named controls in a wpf application.

In C++ we could use the resources file to see a list of all the controls on a form(or so I'm told). I need a similar way to see the a list of controls in a wpf application. The names will be used in a database, so it doesn't matter how I get them, I just need to automate it so I don't miss or misspell any.

Does anyone have any ideas on how to do this? Might there be a tool out there somewhere that could read xaml files and pick out the names?

Upvotes: 0

Views: 1595

Answers (3)

Carlo
Carlo

Reputation: 25959

This will help you out:

private void ProcessLogicalTree(object current)
{
    string elementInfo = current.GetType().Name;

    if (current is FrameworkElement)
    {
        elementInfo += " - Name: " + (current as FrameworkElement).Name;
    }

    MessageBox.Show(elementInfo);

    DependencyObject dependencyObject = current as DependencyObject;

    if (dependencyObject != null)
    {
        foreach (object child in LogicalTreeHelper.GetChildren(dependencyObject))
        {
            ProcessLogicalTree(child);
        }
    }
}

And this is how you use it:

ProcessLogicalTree(this); // Where 'this' is your Window or your UserControl

Preferable on the Loaded event or at a Button_Click event.

Upvotes: 3

Lars Truijens
Lars Truijens

Reputation: 43602

xaml files are a xml format. You can use any xml parser to parse the xaml file. You might notice that not all parts in a xaml file are controls (elements) and controls do not have to have a name specified for it to work.

If you want to do it runtime you could use the LogicalTreeHelper or VisualTreeHelper as already suggested.

Upvotes: 1

David Brunelle
David Brunelle

Reputation: 6430

Someone sent me this earlier:

You can use LogicalTreeHelper (or VisualTreeHelper) to recurse through the WPF trees and find the controls you're interested in.

I don't know if you are using a windows or a ASP.net project, but this is for wpf form project.

Upvotes: 0

Related Questions