Resource loading using Findresource throws exception - WPF / C #

I am writing CustomControl in WPF. I have some DataTemplates in Mymes / Generic.xaml, at the resublcedictionary level, with x: Key assigned to them.

Now, from within the same code in the management class, I want to find and load this resource so that I can dynamically analyze something in the code.

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

It keeps returning that the resource was not found and therefore null.

A resource in it defenitely exists in generic.xaml.

Please, help.

+4
source share
7 answers

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

The resource you are trying to access at the topic level must be identified by ComponentResourceKey to access it from anywhere in the assembly:

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

then in your XAML you refer to 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:

 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 it's the same thing):

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

Please note that even if TypeInTargetAssembly must be installed, it does not restrict access to this resource to other types in the assembly.

+6
source

Make sure you actually add the user control as a child to another control before using FindResource on it. I am sure that when you use FindResource, it goes up the control hierarchy until it finds a match. If your control does not have a parent, it will not find the resource you are looking for.

+3
source

You can download the resource dictionary as follows:

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

Then you can find the resources inside it in the usual way, for example, myDictionary ["keyvalue"]

+2
source

Since you are creating a custom control, I assume that you have a ControlTemplate defined in your generic.xaml? If so, then if you add a 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 pattern has been applied / loaded, you can find these data patterns by calling this .FindResource () inside your control.

+1
source

x: Key and DataType are mutual exceptions. Internally, if you install a DataType, WPF generates a key of type DataTemplateKey. Thus, a call to FindResource with a ComponentResourceKey throws an exception because the resource cannot be found using this key. Use

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

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

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

for a DataTemplate with the key x: Key = {ComponentResourceKey TypeInAssembly = {x: Type l: yourType}, ResourceId = ressId}. Do not define DataType and x: Enter the same template.

+1
source

I'm not sure if I need to define in XAML your usage or static resource with a new x: key that matches what you want to change.

another option is if you use a file containing a template, merge the resources as follows:

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

in the appropriate place where you are going to search for a resource

NTN, Eric

0
source

Thnaks for both.

I tried moving the resources to the CT resource section. Even when I look at runtime, say, in OnApplyTemplate or EndInit (), etc., This.Resources does not have any objects in it :-( although all of its functions control Generic.xaml.

Therefore, it always returns null.

0
source

All Articles