Quantcast
Channel: MVVM Light Toolkit
Viewing all articles
Browse latest Browse all 1826

New Post: getting Locator error at run time when trying to use DataTemplate

$
0
0
Hello,

I seemed to be getting jammed up trying to reference a command in my view model from a data template in another file. I've tried following the examples here and here (as well as others), but the results is always the same. I'm getting a run time error of "{"Cannot find resource named 'Locator'. Resource names are case sensitive."}".

For what it's worth, my DataTemplate file looks like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"
                    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">
    <DataTemplate x:Key="DockPanelHeaderDataTemplate">
        <StackPanel Orientation="Horizontal">
            <Image Source="/Images/Add_16x16.png">
                <Image.RenderTransform>
                    <RotateTransform x:Name="AnimatedRotateTransform" CenterX="8" CenterY="8" />
                </Image.RenderTransform>
                <Image.Triggers>
                    <EventTrigger RoutedEvent="Image.MouseEnter">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:.5"
                                                 From="0"
                                                 Storyboard.TargetProperty="RenderTransform.Angle"
                                                 To="45" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="Image.MouseLeave">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:.5"
                                                 From="45"
                                                 Storyboard.TargetProperty="RenderTransform.Angle"
                                                 To="0" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Image.Triggers>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Image.MouseEnter">
                        <command:EventToCommand Command="{Binding MainWindow.FooCommand, Source={StaticResource Locator}}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Image>
            <TextBlock Margin="10,0,0,0" Text="{Binding}" />
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>
My viewmodel:
using System.Windows;
using GalaSoft.MvvmLight.Command;
using ViewModelBase = GalaSoft.MvvmLight.ViewModelBase;
using IMessageBoxService = DevExpress.Mvvm.IMessageBoxService;

namespace ESPDistributionDemoProgram.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IMessageBoxService messageBoxService)
        {
            _messageBoxService = messageBoxService;
        }

        private RelayCommand _fooCommand;

        public RelayCommand FooCommand
        {
            get
            {
                return _fooCommand
                       ?? (_fooCommand = new RelayCommand(Foo));
            }
        }

        private void Foo()
        {
            _messageBoxService.Show("This is a test.  Should get fired from DataTemplate", "Success!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
        }

        private readonly IMessageBoxService _messageBoxService;
    }
}
My view:
<Window x:Class="ESPDistributionDemoProgram.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
        xmlns:dxdo="http://schemas.devexpress.com/winfx/2008/xaml/docking"
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
        xmlns:ignore="http://www.ignore.com"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:views="clr-namespace:ESPDistributionDemoProgram.Views"
        Title="Distribution Modeller"
        Icon="Images/robot1.ico"
        WindowState="Maximized"
        d:DesignHeight="1394"
        d:DesignWidth="1460"
        dx:ThemeManager.ThemeName="LightGray"
        mc:Ignorable="d ignore">
    <Window.DataContext>
        <Binding x:Name="This"
                 Path="MainWindow"
                 Source="{StaticResource Locator}" />
    </Window.DataContext>
    <dx:BackgroundPanel>
        <Grid>
            <dxe:FlyoutControl Content="{Binding Path=PlacementTarget.Tag,
                                                 RelativeSource={RelativeSource Self}}"
                               PlacementTarget="{Binding ElementName=HxINfoPanel}"
                               Style="{StaticResource CustomFlyoutControlStyle}" />
            <dxdo:DockLayoutManager FloatingMode="Desktop">
                <dxdo:LayoutGroup>
                    <dxdo:LayoutGroup Orientation="Vertical">
                        <dxdo:LayoutPanel x:Name="HxINfoPanel"
                                          AllowClose="False"
                                          AllowMinimize="True"
                                          Caption="Historical Information"
                                          CaptionTemplate="{StaticResource DockPanelHeaderDataTemplate}">
                            <views:HistoricalDataView VerticalAlignment="Top" />
                        </dxdo:LayoutPanel>

                        <dxdo:LayoutPanel AllowClose="False"
                                          AllowMinimize="True"
                                          Caption="Distribution Information"
                                          CaptionTemplate="{StaticResource DockPanelHeaderDataTemplate}">
                            <views:DistributionView VerticalAlignment="Top" />
                        </dxdo:LayoutPanel>
                    </dxdo:LayoutGroup>

                    <dxdo:LayoutPanel AllowClose="False"
                                      AllowMinimize="True"
                                      Caption="Simulation Information"
                                      CaptionTemplate="{StaticResource DockPanelHeaderDataTemplate}">
                        <views:SimulationView />
                    </dxdo:LayoutPanel>
                </dxdo:LayoutGroup>
            </dxdo:DockLayoutManager>

        </Grid>
    </dx:BackgroundPanel>




</Window>
and my ViewModelLocator:

using DevExpress.Xpf.Core;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;

namespace ESPDistributionDemoProgram.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class ViewModelLocator
    {
        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            SimpleIoc.Default.Register(() => new MainViewModel(new DXMessageBoxService()));
            SimpleIoc.Default.Register<DistributionViewModel>();
            SimpleIoc.Default.Register(()=> new HistoricalDataViewModel(new DXMessageBoxService()));
            SimpleIoc.Default.Register<SimulationViewModel>();

        }

        public HistoricalDataViewModel HistoricalData
        {
            get { return ServiceLocator.Current.GetInstance<HistoricalDataViewModel>(); }
        }

        public DistributionViewModel Distribution
        {
            get { return ServiceLocator.Current.GetInstance<DistributionViewModel>(); }
        }

        public SimulationViewModel Simulation
        {
            get { return ServiceLocator.Current.GetInstance<SimulationViewModel>(); }
        }

        public MainViewModel MainWindow
        {
            get { return ServiceLocator.Current.GetInstance<MainViewModel>(); }
        }

   
        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
        }
    }
}
I've narrowed down the issue to be specifically in my DataTemplate on this line:
<command:EventToCommand Command="{Binding MainWindow.FooCommand, Source={StaticResource Locator}}" />
The Intellisense works, with I type that line...so Visual Studio knows that the Locator exists, but not the compiler. I'm hoping it's something simple that I'm over looking, but I just can't seem to put my finger on it.

Any help you can provide is greatly appreciated.

Thanks in advance

Viewing all articles
Browse latest Browse all 1826

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>