Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find resource named 'X', why not?

Tags:

mvvm

wpf

xaml

I want to create a wizard UI for inventories management. The relevant line in the xaml is:

<ContentPresenter Content="{Binding Current}" ContentTemplateSelector="{StaticResource inventorySelector}"/>

"Current" is the currently active view model, one of AvailableInventoriesViewModel, GroupsViewModel, NewArticlesViewModel, ResultViewModel. The DataTemplateSelector I have defined as such:

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using Centron.WPF.WarehousingExtension.InventoryModule.ViewModels.WizardViewModels;

namespace Centron.WPF.WarehousingExtension.InventoryModule.UI.DataTemplateSelectors
{
    public class InventoryDatatemplateSelector : DataTemplateSelector
    {
        public DataTemplate AvailableDatatype { get; set; }
        public DataTemplate GroupsDatatype { get; set; }
        public DataTemplate NewDatatype { get; set; }
        public DataTemplate ResultDatatype { get; set; }

        public InventoryDatatemplateSelector()
        {
            Debug.WriteLine("");
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item is AvailableInventoriesViewModel)
                return AvailableDatatype;
            else if (item is GroupsViewModel)
                return GroupsDatatype;
            else if (item is NewArticlesViewModel)
                return NewDatatype;
            else return ResultDatatype;
        }
    }
}

Then I create instances of the DataTemplates and a Selector like this:

<base:InventoryViewBase.Resources>
    <DataTemplate DataType="viewModels:AvailableInventoriesViewModel" x:Key="availableInventoriesDatatype">
        <controls:AvailableInventoriesView />
    </DataTemplate>
    <DataTemplate DataType="viewModels:GroupsViewModel" x:Key="groupsDatatype">
        <controls:GroupsView />
    </DataTemplate>
    <DataTemplate DataType="viewModels:NewArticlesViewModel" x:Key="newArticlesDatatype">
        <controls:NewArticlesView />
    </DataTemplate>
    <DataTemplate DataType="viewModels:ResultViewModel" x:Key="resultDatatype">
        <controls:ResultView/>
    </DataTemplate>
    <selector:InventoryDatatemplateSelector
            x:Key="inventorySelector" 
            AvailableDatatype="{StaticResource availableInventoriesDatatype}"
            GroupsDatatype="{StaticResource groupsDatatype}"
            NewDatatype="{StaticResource newArticlesDatatype}"
            ResultDatatype="{StaticResource resultDatatype}"/>
</base:InventoryViewBase.Resources>

I set a breakpoint in the constructor of my InventoryDatatemplateSelector, and can step through it, but in the next Debug step, apparently when it tries to set the first property of that selector instance, I immediately get an exception with inner exception:

Cannot find resource named \"availableInventoriesDatatype\". Resource names are case sensitive.

What's the deal, why is the resource not found when it's clearly defined?

like image 440
Hackworth Avatar asked Mar 21 '13 16:03

Hackworth


1 Answers

Ok, I found the solution. The only error was that the "Key" property of a resource has to be set first. So instead of:

    <DataTemplate DataType="viewModels:AvailableInventoriesViewModel" x:Key="availableInventoriesDatatype" >
        <controls:AvailableInventoriesView />
    </DataTemplate>

I need:

    <DataTemplate x:Key="availableInventoriesDatatype" DataType="viewModels:AvailableInventoriesViewModel" >
        <controls:AvailableInventoriesView />
    </DataTemplate>
like image 156
Hackworth Avatar answered Oct 13 '22 06:10

Hackworth