Sunday, 29 April 2012

TryFindResource Silverlight

A nice hierarchical resource search in Silverlight I came across in on internet, returns null if the resource cannot be found.

public static class FrameworkElementExtensions
{
public static object TryFindResource(this FrameworkElement element, object resourceKey)
{
FrameworkElement currentElement = element;

while (currentElement != null)
{
object resource = currentElement.Resources[resourceKey];
if (resource != null)
{
return resource;
}

currentElement = currentElement.Parent as FrameworkElement;
}

return Application.Current.Resources[resourceKey];
}
}

Original post:  http://blog.functionalfun.net/2011/01/findresource-implementation-for.html

No comments:

Post a Comment