tags:

views:

2576

answers:

5

I am writing a CustomControl in WPF. I have some DataTemplates in my Themes/Generic.xaml, at the resourcedictionary level, with x:Key assigned for them.

Now from within the same control class code, i want to find and load that resource so i can dynamically assing to something in the code.

I have tried base/this.FindResource("keyvalue"), this.Resources[""] etc.

It keeps returning that the resource is not found and hence null.

The resource is defenitely there in the generic.xaml.

Please help.

A: 

Hi, Im not sure i think you need to define in the of the XAML your using or the a static resource with a new x:Key that corrisponds to what you want to change.

another option is if you use a file that contains the template merge the resources like this:

<ResourceDictionary.MergedDictionaries>
  <ResourceDictionary Source="SomeTemplate.xaml"/>
</ResourceDictionary.MergedDictionaries>

in the appropriate place your going to try and find the resource

HTH, Eric

A: 

Since you're building a custom control I presume that you have a ControlTemplate defined in your generic.xaml file? If so, then if you add your DataTemplate(s) to the Resources section of the ControlTemplate like this:

<ControlTemplate>
    <ControlTemplate.Resources>
        <!-- Data Templates Here -->
    </ControlTemplate.Resources>

    <!-- Rest of Control Template -->
</ControlTemplate>

then provided that the control template has been applied/loaded then you'll be able to find that data templates using a call to this.FindResource() within your control.

Richard C. McGuire
A: 

Thnaks to both.

I tried moving the resources into CT's resources section. Even then when i look up at runtime, say in OnApplyTemplate or EndInit() etc., the this.Resources has no objects in it :-( although its all ther ein the control's Generic.xaml.

So it returns null at all times.

+2  A: 

Make sure that you have actually added the custom control as a child to another control before using the FindResource on it. I'm pretty sure that when you use FindResource, it climbs the control hierarchy until it finds a match. If your control has no parent, it will not find the resource you are looking for.

timothymcgrath
+2  A: 

A bit late for an answer, but it might benefit the others.

The resource you're trying to access is at the theme level, to access it from anywhere in your assembly it must be identified by ComponentResourceKey:

<Style TargetType="{x:Type TreeViewItem}" 
       x:Key="{ComponentResourceKey {x:Type local:MyTVIStyleSelector}, tviBaseStyle}">
  <!-- style setters -->
</Style>

then in your XAML you'd reference it like this:

<Style TargetType="{x:Type TreeViewItem}" 
       x:Key="{ComponentResourceKey {x:Type local:MyTVIStyleSelector}, tviStyle_1}"
       BasedOn={StaticResource {ComponentResourceKey {x:Type local:MyTVIStyleSelector}, tviBaseStyle}}>
  <!-- style setters -->
</Style>

and in your code like this:

ComponentResourceKey key = new ComponentResourceKey(typeof(MyTVIStyleSelector), "tviStyle_1");
Style style = (Style)Application.Current.TryFindResource(key);

There is also a verbose form of XAML syntax that looks like this (but its just the same thing):

<Style TargetType="{x:Type TreeViewItem}" 
       x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyTVIStyleSelector}, ResourceId=tviBaseStyle}">
  <!-- style setters -->
</Style>

Note that even though the TypeInTargetAssembly must be set it does not restrict access to this resource for other types in assembly.

Alex_P