WPF: Make Application Resources override usercontrol library resources - wpf

I have a wpf application project, with user controls in a separate project.
I'd like to have different resource dictionaries in the different projects
When I view the user controls from the user control project, I'd like to use the user control library's dictionary.
when I run the Application, i'd like to use the application's dictionary.
Right now, when i run the application, the application still has it's own dictionary, but the user controls are still using the user control library's dictionary
I've tried combinations of dictionaries and merged dictionaries.
Not really sure how to do this.
thanks in advace

So I found a workaround:
Include an App.xaml file in the user control library. Define the user control design time resources in the user control App.xaml file
In the regular wpf application project, include the run time resources.
At design time, the user controls only see its own App.xaml file, while at run time, the appliction is run and uses the resources defined in the application projects App.xaml file
Don't know if this is the optimal method of doing this. If anyone has any comments on doing it this way, please feel free to post
Thanks

Related

Resources from ResourceDictionary accessible but Styles are not

I'm hoping to create a WPF User Control Library with reusable controls for my many projects to save time recreating them every time (something I've found myself doing).
So I:
Created a WPF User Control Library, and created a ThemeDark.xaml file to contain common colour keys and styles.
Created a separate regular WPF project in the same solution so that I can test the controls in an actual window.
Added a project reference to the User Control Library.
Added a ResourceDictionary.MergedDictionaries into the App.xaml which references the theme file from the other project.
The problem: I can access and use the SolidColorBrush keys from the theme file just fine in the testing project, but for some reason IntelliSense can't see the Styles (which all have keys).
I also can't seem to use the pack://application:,,,/ method of referencing the ResourceDictionary in App.xaml; it only accepts the filename. This was originally set through the Properties panel -> Source -> Browse, however I have tried a few different formats, all to no avail. It also doesn't seem to notice that ThemeDark.xaml is in a Themes/ directory within the User Control Library project.
So - How does one properly reference a ResourceDictionary from a WPF User Control Library project and be able to use all resources (including Styles)?
DarkTheme.xaml (inside user control library project):
App.xaml (inside testing project):
Resulting behaviour (very strange - SolidColorBrushes available but Styles aren't):
I found the problem. I had removed the spaces from the project's namespace, but not from the assembly name.
Once that was fixed I was able to use Source="pack://application:,,,/MyControlLibrary;component/Themes/ThemeDark.xaml"
MyControlLibrary isn't the actual name! ;)

In WPF Project I have separate folder for each module I want default app.xaml file to be loaded whenever I hit that module screen

enter image description hereI am migrating my project from Silverlight to WPF and In Silverlight each module has different project and it's own configuration file i.e. App.xaml. But while migrating it in my WPF project I have separate folder for each modules. In WPF we are adding silverlight library files into WPF folder. So how we can initialize app.xaml file in WPF at folder level?
The answer to " So how we can initialize app.xaml file in WPF at folder level?" is:
You cannot do that.
You might be able to do something similar will satisfy your requirements. Assuming you're talking resources.
Consider a single resource in a resource dictionary with an x:Key of "XX".
When you merge that in app.xaml it's added to a hashtable with a key of XX and value of whatever your resource is. That goes into Application.Current.Resources.
If your wpf app then loads any usercontrol, window whatever that uses a dynamicresource XX then it gets that value. Doesn't matter where it is loaded from. That could be a different library or whatever you like.
Resources have scope.
You could therefore merge a resource dictionary at window level. Say you added another resource dictionary in a module. You merge it into Window1's resources and it has something with a x:Key of "XX". Anything in that instance of Window1's visual tree will grab that "new" XX you just merged in. Anything in any other window will still be using that one out of Application.Current.Resources.
You could conceivably merge resource dictionaries at usercontrol level in this way. This is usually a bad idea though because each gets an instance in memory of your resources. That can add up to a lot of memory if you're not careful.
You could therefore arrange things so each module has it's own window ( or some other parent ) which merges in it's own somehow unique resource dictionary. That supplies a more local version of any resources you need.
Or
Maybe the user only navigate to and hence sees one module at a time. Whatever a module is.
You could then merge your resource dictionaries in at application level. Everything gets them then though.

Loading WPF Resources more than once

I am working in WPF and WinForms for both Windows XP and Windows 7/10 users (.NET 4.0 due to XP).
Some WPF Windows are used as dialogs for older WinForms applications without a WPF Application class/App.xaml file.
This in itself isn't a problem but I find I'm having to declare styles in resource dictionaries in user controls/windows in the WinForms/WPF situations which isn't normally necessary in plain WPF applications due to App.xaml.
My question is whether WPF detects that the same resource is being loaded twice (in a pure WPF application e.g. UserControl and App.xaml) and copes with/manages this without interference from me or do I have to try to ensure I only declare resource dictionaries once?
Many thanks.
The answer is no.
If you load a resource dictionary as a resource for a usercontrol then each instance of the usercontrol means another instance of any resources it uses in memory.
If that is going to be a problem then you need to do something.
You might just be able to instantiate an application object and use that to stash your resources in. Application.Current.Resources is after all just referencing the current application. Depends on exactly how your app works.
If you try to load the same resource dictionary twice, the application will throw a runtime error.
But, if you are only defining the styles already present in the resource dictionary, in the user control also, i.e, basically two styles with the same name one in the resource dictionary and the other in the user control, then no error is thrown. The style in the user control will have a higher priority.

Applying global styles to inherited Windows controls in control library

I'm a complete n00b to WPF but I'm working on my first application. I already realize that styles I use in the application I will likely want to use in future applications, so I'd like to use some method of applying global styles from project to project.
I've seen plenty of tutorials on creating a control library project, but they all go into creating custom controls. I don't really need custom controls (yet) per se, just the standard Windows controls with custom styles.
I'm also a little unclear on the whole ResourceDictionary thing. I've found examples on creating one for an application project, but not so much for a control library project.
What I'm looking for here is a) is a control library really what I need or am I creating more work than necessary? b) am I on the right path with a ResourceDictionary? and c) any good links to tutorials/examples that might go into what I'm trying to do rather than just a custom control creation tutorial.
You definitely want a ResourceDictionary with styles that you will be using in other apps. You can then reference it on an application, window, or even control level by including it in the application, window or control resources.
Where you put that resource dictionary isn't that important, though a custom control project is a common place to do so. It can be anywhere, in any project, and you can reference it with a URI.
Microsoft has a pretty good writeup on resources: Using Resources. Here's a decent tutorial on using dictionaries: Resource Dictionaries
You are in the right direction with ResourceDictionary. Create one for your application in a separate library move all your Styles there and refer them using Pack URI syntax. Here is a related question: ResourceDictionary in a separate assembly

Resource Dictionaries in a Silverlight Assembly?

I've just begun dabbling in putting together a set of controls as assemblies and I'm working on default styling. What I currently have is a UserControl in a project (thanks Reed!) and I'm able to bring that into another project via reference. I plan to add more controls over time to build something of an SDK.
I currently have some hooks that look for resources in the hosting application which either apply the resources to their respective properties, or style out the control via hard coded defaults.
Is it possible to set up resource dictionaries within the project containing the UserControls so they can use those references as the default, instead of hard coding? If so, how do I target them?
(I have a ResourceDictionary set up within the same project as the controls: Resources>Dictionaries>Colors.xaml)
Thanks in advance!
E
You should really look at creating custom templated controls in library rather than derivatives of UserControls. This will allow projects that reference your library to specify an alternative default style for you controls in the same way as we can for the controls in Microsofts own SDK.
The Creating a New Control by Creating a ControlTemplate topic on MSDN is good starter.
I think this is a better explanation, but i'm trying on a desktop application and i got the same problem.
XamlParseException: Failed to create a 'System.Type' from the text 'local:CustomerEntity'
If I'm undestanding correctly you want to create the file "generic.xaml" in the folder "Themes". However, I don't believe automatic styling works with UserControl only with Control. Generally if you trying to make a control that can be stylized and retemplated you want to inherit from Control and not UserControl.

Resources