Search code examples
wpfc#-4.0resourcedictionaryapp.xamlviewmodellocator

How to resolve "..dictionary entry must have associated key" error?


I've added a namespace to my App.xaml file in order to resolve my ViewModelLocator.cs location in the project.Then referenced the ns from a ResourceDictionary. But I get two errros when I add these:

..Each dictionary entry must have an associated key.

'ViewModelLocator' does not exist in XML namespace 'clr-namespace:MongoDBApp.ViewModels;assembly=MongoDBApp'

I've checked firstly that the namespace is correct for the location of the ViewModelLocator, which is: namespace MongoDBApp.ViewModels.

I also checked the syntax on the reference in the ResourceDictionary which seems correct. This solution didn't resolve the error and I've cleaned and rebuilt the solution a few times.

Can anyone advise on how to resolve this error?

The definition of the App.xml file is as follows, the ResourceDictionary is near the bottom of the file:

<Application x:Class="MongoDBApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:converters="clr-namespace:MongoDBApp.Converters"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:MongoDBApp.ViewModels;assembly=MongoDBApp"
             xmlns:validators="clr-namespace:MongoDBApp.Validator"
             StartupUri="pack://application:,,,/Views/MainView.xaml"
             d1p1:Ignorable="d">
    <Application.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel>
                            <Grid Width="16"
                                  Height="16"
                                  Margin="3 0 0 0"
                                  VerticalAlignment="Center"
                                  DockPanel.Dock="Right">
                                <Ellipse Width="16"
                                         Height="16"
                                         Fill="Red" />
                                <Ellipse Width="3"
                                         Height="8"
                                         Margin="0 2 0 0"
                                         HorizontalAlignment="Center"
                                         VerticalAlignment="Top"
                                         Fill="White" />
                                <Ellipse Width="2"
                                         Height="2"
                                         Margin="0 0 0 2"
                                         HorizontalAlignment="Center"
                                         VerticalAlignment="Bottom"
                                         Fill="White" />
                            </Grid>
                            <Border BorderBrush="Red"
                                    BorderThickness="2"
                                    CornerRadius="2">
                                <AdornedElementPlaceholder />
                            </Border>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                </Trigger>
            </Style.Triggers>
        </Style>
        <ResourceDictionary>
            <local:ViewModelLocator x:Key="mainViewModelLocator" ></local:ViewModelLocator>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Brown.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Brown.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>     
    </Application.Resources>
</Application>

Solution

  • Something like this should work, please note my ViewModelLocator in this case comes from prism (this is why I need IView, you don't if you use something else).

    Base class

    public class MyFormUserControl : UserControl, IView
    {
        public MyFormUserControl()
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                SetValue(ViewModelLocator.AutoWireViewModelProperty, true);
            }
        }
     }
    

    UserControl

    <controls:MyFormUserControl  x:Class="MyWpf1.UserControl1"
                             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                             xmlns:controls="path to the base class">
    
    your usual xaml goes here
    
    </controls:MyFormUserControl>
    

    Code behind

    public partial class UserControl1: MyFormUserControl
    {
        public CrateFormView() : base()
        {
            InitializeComponent();
        }
    }