I have been trying for a day now, to no avail, to create a bunch of brushes in the theme then using them with DynamicResource in a custom control. What I did is this:
So my problem is that I can't find any way to define the default values in the theme so that I can change them programatically in the application. How can StaticResource find the brush and DynamicResource not?!
I must add that I've created a static class holding the component resource keys as properties that I then use in the xaml as {x:Static UI:ResourceScheme.ControlBackgroundKey} for example. My problem seems similar to this one: ComponentResourceKey as DynamicResource problem only that if I replace the static property keys to the XAML markup for component resource key, it still doesn't work.
Can someone please help me out here? :(
WPF - StaticResource vs DynamicResource. The difference between StaticResource and DynamicResource lies in how the resources are retrieved by the referencing elements. StaticResource are retrieved only once by the referencing element and used for entire life of the resource. On the other hand,DynamicResource are acquired every time...
The difference between StaticResource and DynamicResource lies in how the resources are retrieved by the referencing elements. StaticResource are retrieved only once by the referencing element and used for entire life of the resource. On the other hand,DynamicResource are acquired every time the referenced object is used.
You can define DynamicResource in XAML as well, but it will resolve at runtime only. I hope this article has cleared your confusion between static and dynamic resources in WPF. Please apply them to achieve reusability in your projects.
This key was initially assigned by the x:Key Directive if a resource was created in markup, or was provided as the key parameter when calling ResourceDictionary.Add if the resource was created in code. A StaticResource must not attempt to make a forward reference to a resource that is defined lexically further within the XAML file.
Here is the difference,
StaticResource loads at time of loading, this means that the resource key that you are using, must be lexically defined before the usage.
So, static resource in case of custom control must be defined only above the control definition in the same generic.xaml file. So if you put your brushes in different xaml, it will certainly not work in case of static resource.
This is the reason, unless the other resources of type xaml is included in the form of some sort of import at a time of complile in same file, you can not use static resource in the file. It simply means that the file/component/control's actual xaml some how should contain actual reference of static resource you use.
Now I have my doubt of why DynamicResource will not work, that is because probably DynamicResource will only look in the Application's (where the control is use) ResourceDictionary but not generic.xaml.
I am not 100% sure but I feel if you define a custom control and if you use DynamicResource then your resources has to be in the Application's Resource Dictionary or the parent container of your control's resource dictionary, but it can not be in generic.xaml.
Because DynamicResource will only look up for keys in the logical tree of control's runtime and thats why it may not find resources that are in generic.xaml unless generic.xaml is explicitly added in Application.Resources.
Summary: StaticResource must be available lexically before in the same file at compile time, resources will be available in Application.Resources dictionary, it still can find in logical tree but at a compile time only in the same dll or same generic.xaml.
DynamicResource must will be searched in Application.Resources and in the logical tree of the control at runtime.
For more reference, please check Resources Overview
Finally fixed it. It appears that having the type for the component resource key in another assembly caused the whole issue. Let me summarize:
The end result for this is:
The solution for this seems to be to move the resources class in the controls library.
As I still do not know why this is happening, this question remains open, even if slightly changed: why doesn't it work in the first scenario?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With