4

My concern is how to handle ResourceDictionarys in a WPF (MVVM)-Project. I usually only create small Projects, so it doesn't matter that much, but since I created a Project-Template to handle some of the boilerplating, I'm really interested into this Topic.

I found the following approach on Stack Overflow: “Resource Dictionary (WPF)” naming convention and managing suggestions

The User suggests the following Dictionaries:

  • Fonts and Font-Sizes (Fonts.xaml)
  • Brushes and Colors (Brushes.xaml)
  • Generic Control Styles, without keys (CoreStyles.xaml)
  • Data Templates and TemplateSelectors (DataTemplates.xaml)
  • Converters (Converters.xaml)
  • BitmapImages for use as Image sources (Icons.xaml)
  • Specific, keyed styles (Styles.xaml)

I created them and just loaded them all on the App_Start, but now it gets dicy: Since I'd like to define my assignments as StaticResource (for performance reasons, from what I read), I need to load them in the exact correct order, as described here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/3bea80f9-d1db-4cb7-ae7a-77a02eaf4ec9/resourcedictionary-load-order?forum=wpf.

This causes some "Egg-Chicken"-problems for me, since in the Styles.Xaml, I have stuff like:

<Style TargetType="Button" x:Key="StyleButtonNavigation" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Margin" Value="0,0,0,10" />
</Style> 

So, I'd have to load CoreStyles before Styles, but CoreStyles then uses other stuff as well etc, and that's just the simplest of a possible application.

Another approach I found was to create a ResourceDictionary for each Style etc.: https://projekt202.com/blog/2010/xaml-organization/ This seems to be the approach, which corresponds most to the SRP, but I'm not seeing this scaling well into a larger project.

Interesting enough, I didn't find any guidelines or best practices from Microsoft about this topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.