Creating controls in WPF seems difficult at first. The separation between code and the appearance of the control is one of the great WPF features, but it requires a different way of thinking about controls. The code is located in its own folder and namespace, but the control default style is defined in the Generic.xaml file (or one of its themed cousins) that is located in the project’s Themes directory.
Although the behaviour (code) and its appearance (style) are completely separated, you still work on both of them at the same time. So it would be convenient to have these files close together. Another problem of the Generic.xaml file is that it tends to get very large, when you have a lot of (complex) controls. Fortunately, there is a good way to solve this issue.
Default styles need to be defined in the Theme\Generic.xaml (or one of its themed cousins). Nothing you can do about that, but you can make the generic resource dictionary a merged dictionary that loads other dictionaries that you can put on arbitrary places.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/RamonDeKlein.Controls;component/AsyncLoadControl/AsyncLoadControl.xaml"/> <ResourceDictionary Source="/RamonDeKlein.Controls;component/AnimatedSizeControl/AnimatedSizeControl.xaml"/> <!-- include other definitions here --> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
You can create separate resource dictionaries for your controls and put them in the same folder as your code’s control. Make sure you set the build action for the resource dictionary to ‘Resource’.
A drawback of this solution is that you need to specify the assembly name for each resource dictionary that you want to merge. This is a flaw in WPF when using merged dictionaries in Generic.xaml.