views:

147

answers:

2

I have a ResourceDictionary called CustomStyles.xaml within in my project. It is located in a subdirectory called RD, therefore its location is RD/CustomStyles.xaml.

My question is the following: I have a class-only file called CustomGroupBox.vb, and in the New(), I would like to access resources from the ResourceDictionary.

How can I do this since I have no corresponding XAML for CustomGroupBox.vb?

P.S. I'd like to note that CustomStyles.xaml has a BuildAction of Resource and not Content, therefore it is compiled along with the project, it does NOT output to the build directory (\bin). Therefore the following wouldn't work...

Me.Resources.Source = New Uri("RD\CustomStyles.xaml")
A: 

You can just pull the resource from the assembly.

This blog post will show you how.

David Basarab
+1  A: 

Seems like you need to use Application.Current.Resources to find your resources, assuming the resource dictionary is defined at the Application level. Example:

YourControl.Style = CType(Application.Current.Resources("OneOfYourStyles"), Style)

Edit: For a resource in an assembly and not the application, you can use Pack URI Syntax (link) to access the resource. It would probably look something like this:

Dim u as Uri = New Uri("/" + Me.GetType().Assembly.GetName().Name + ";RD/CustomStyles.xaml"
Ben Collier
Sounds like someihing I did once, but i didn't need the 'Current' since I was in the file itself.
David Brunelle
Actually, this didn't work for me. Since my files are in an assembly and not an application, I get the following error:'Current' is not a member of 'MyApplication'
Max
Ah, I see. I think you can use Pack URI syntax to get a resource from an assembly. Edited my answer with details. Hope that helps!
Ben Collier