4

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.

7 Answers 7

7

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.

1
3

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.

2

You can load the resource dictionary like this:

ResourceDictionary myDictionary = Application.LoadComponent(new Uri("/MyAssembly;component/Themes/Generic.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary;

You can then find resources within it in the usual manner, e.g., myDictionary["keyvalue"]

1

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.

1

x:Key and DataType are mutualy exclusiv. Internally, if you set DataType, WPF generates a key of type DataTemplateKey. So a call of FindResource with ComponentResourceKey throws an exception, because the resource can't be found with this key. Use

frameworkElement.FindResource(new DataTemplateKey(typeof(yourType)));

for a DataTemplate with DataType={x:Type local:yourType} defined or

frameworkElement.FindResource(new ComponentResourceKey(typeof(yourType), "ressId"));

for a DataTemplate with x:Key={ComponentResourceKey TypeInAssembly={x:Type l:yourType}, ResourceId=ressId} defined. Don't define DataType and x:Key in the same template.

0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.