Reputation: 1579
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
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
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
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