2

I'm about to create multiple projects with WPF. I'm using styles to modify the look and feel of the applications and put them to the app.xaml.

But now i have the problem that i need to have the same styles in every project. Because copy and paste of the app.xaml content to every project is a very bad idea I'm looking for a solution to centralize the app.xaml to access the styles from every project.

Just code
  • 13,553
  • 10
  • 51
  • 93
SyLuS
  • 103
  • 1
  • 10
  • Possible duplicate: [Copy file(s) from one project to another using post build event](http://stackoverflow.com/questions/11001822/copy-files-from-one-project-to-another-using-post-build-event-vs2010) – Claudio P Jan 20 '16 at 10:18
  • No, i dont want to use post build events and copy arround the files between projects. Having a central project which can be referenced would be much better. Also a solution would be to have an external file where i put into all the necessary declarations used for the applications. – SyLuS Jan 20 '16 at 10:44

1 Answers1

5

You might create a common styles file in one of your project. For example, this might be a GenericStyles.xml file containing your styles and resources.

In other projects, you could add this file as link: right click on a project -> Add -> Existing Item... -> select GenericStyles.xml file -> open the drop-down menu on the Add button -> select Add As Link.

So now you have a single file that will be part of all your projects.

In each project's app.xaml, you could add your styles and resources using this markup:

<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="GenericStyles.xml"/>
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Now, you would have to change only one file in the "main" project, and the other projects would automatically apply these changes.

dymanoid
  • 14,771
  • 4
  • 36
  • 64
  • 1
    Perfect. Thank you for the very nice Step-by-Step guidance. Thats exactly what I searched for. Easy way to handle a central resource file shared by serveral solutions/projects. I would wote up youre answer ... but still dont have the resputation for it =( – SyLuS Jan 20 '16 at 11:14
  • 1
    But the resource is not linked at designtime. So im getting errors in the disigner because im referencing the style using BasedOn="{StaticResource myButtonStyle}" to extend the style with triggers. References of the colors can be a DynamicResource but BasedOn dosent allow DynamicResources. Running the application works fine. – SyLuS Jan 20 '16 at 11:27
  • @SyLuS, yes, this might be the case. I didn't check all the possible resource usages. The XAML designer is pretty unstable and sometimes fails for strange reasons. You might open an issue on the Visual Studio support site for this. – dymanoid Jan 20 '16 at 12:10