Have a look at the VisualTreeHelper (I think that's it's name, I'm not near a real internet connection - am on a cruise ship anchored off Geraldton!) which has some static methods for finding child or parent elements etc

Cheers,
Stephen (from mobile)

On 24/01/2010, at 8:16 AM, Paul Stovell <[email protected]> wrote:

I should have checked first, apparently it's not supported. I don't know how you SL guys can inflict so much pain upon yourselves :)

I suppose it could be created by something like this:

public static T FindResource<T>(this FrameworkElement element, string name)
{
     var resource = element.Resources[name];
     if (resource != null)
     {
         return (T)resource;
     }

     var parent = element.Parent as FrameworkElement;
     if (parent == null) throw new Exception();

     return parent.FindResource<T>(name);
}




On Sun, Jan 24, 2010 at 10:06 AM, Paul Stovell <[email protected]> wrote:
Hi Tony,

I presume Silverlight has a FindResource()? In WPF that walks up the visual tree looking for the resource, so it should find the item on the page.

Paul



On Fri, Jan 22, 2010 at 10:15 PM, Tony Wright <[email protected]> wrote: Hmmm. That works if I have the resource specified as a UserControl.Resource within the control, but not if the resource is on the owning page – it comes back null.



The user control requires access to various ViewModel classes, as there are combo boxes to populate, so I am reluctant to bind the UserControl.DataContext to the PriceViewModel, as it is only one of a number of similar ViewModels needed. But I will have a rethink about this one.



The other thought was whether there was some way to centralise access to ViewModel classes from within the application. That is, has anyone managed to put their ViewModel classes in as Application.Resources?



T.



From: [email protected] [mailto:ozsilverlight- [email protected]] On Behalf Of Miguel Madero
Sent: Friday, 22 January 2010 11:10 PM
To: ozSilverlight
Subject: Re: Accessing page resources from user controls



var viewModel = (PriceViewModel)Resources["YourKey"];

Since your UC is inside the page it will get the resources from there.



Are you setting the ViewModel as the DataContext. If so, you can also get it from there.

--
Miguel A. Madero Reyes
www.miguelmadero.com (blog)
[email protected]


_______________________________________________
ozsilverlight mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight




--
Paul Stovell



--
Paul Stovell
_______________________________________________
ozsilverlight mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
_______________________________________________
ozsilverlight mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight

Reply via email to