I've created a small helper class with a single method to do the reverse lookup that you require.
public static class ResourceHelper
{
static public string FindNameFromResource(ResourceDictionary dictionary, object resourceItem)
{
foreach (object key in dictionary.Keys)
{
if (dictionary[key] == resourceItem)
{
return key.ToString();
}
}
return null;
}
}
you can call it using the following
string name = ResourceHelper.FindNameFromResource(this.Resources, defaultStyle);
Every FrameworkElement
has it's own .Resources
dictionary, using 'this' assumes you're in the right place for where MyTestStyle is defined. If needs be you could add more methods to the static class to recursively traverse all the dictionaries in a window (application ?)