Resource Dictionary + ViewModelLocator

May 20, 2011 at 9:40 PM

I recently started playing with MVVM (new to WPF too - quite the learning curve for all of this) and everything was working great with my ViewModelLocator instance and binding design-time for VS2010 until I started using the MetroToolkit provided on CodePlex. Before utilizing the toolkit, I had the following:

<Application.Resources>
        <local:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </Application.Resources>  

All of my views were binding great and everything looked really good. I was surprised at just how easy someone without MVVM (or MVC) experience could get up and running. Then I hit a snag of MetroToolkit requiring merged resource dictionaries and now no matter what I try I can't get VS to find my ViewModelLocator again inside App.xaml. Here is the new markup:

<Application.Resources>

        <ResourceDictionary>
            
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Colors.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Animations.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Geometry.xaml"/>
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Generic.xaml"/>
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Buttons.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Scrollbar.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Scrollviewer.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/RadioButton.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/ProgressBar.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/ContextMenu.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Tooltip.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Checkbox.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Headings.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Textbox.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Combobox.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Slider.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/Expander.xaml" />
                <ResourceDictionary Source="/MetroToolkit;component/Themes/Dark/TabControl.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <local:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
            
        </ResourceDictionary>
    </Application.Resources>    

I have tried giving the resource dictionary a key, adding the <local:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> line outside of the <ResourceDictionary /> area (above and below - throws nasty and very unhelpful errors) and can't get it to find my VM Locator. It works immediately when I remove the <ResourceDictionary /> block from App.xaml, but based on my very limited knowledge of WPF I need those if I want the styles to be available to all views in my application.

Any thoughts? This has been driving me crazy for a few hours now.

May 25, 2011 at 6:21 PM

Anyone?

May 30, 2011 at 2:05 PM

For anyone else who runs into this, I received an answer on StackOverflow that worked perfectly (http://stackoverflow.com/questions/6156154/mvvm-light-viewmodellocator-resourcedictionaries):

<ResourceDictionary>
	<ResourceDictionary.MergedDictionaries>
		<ResourceDictionary Source="Dictionaries/converters.xaml" />
		<ResourceDictionary Source="assets/styles.xaml" />
		<ResourceDictionary Source="assets/sdkstyles.xaml" />
		<ResourceDictionary Source="assets/corestyles.xaml" />
		<ResourceDictionary>
			<!-- Put Locator here -->
			<local:ApplicationResources x:Key="ApplicationResources" />
		</ResourceDictionary>
	</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>