I'm having a hard time figuring out 'How' a view and view model gets hooked up. Where does the view datacontext actually get assigned to the instance of the view model?

I know to just add the right imports and exports will do the trick, but I'm trying to figure out the 'How'.

thanks.

jim

Follow up:

Scott,

How does this work if you want 2 open instances of a view model? (2 documents with different content)

How does the WPF system know which instance of a viewmodel to hook to a view?

jim

asked 24 Sep '10, 12:31

jimmy's gravatar image

jimmy
31114
accept rate: 0%

edited 27 Sep '10, 07:44


Take a look at App.xaml.cs, specifically at the OnImportsSatisfied method:

    public void OnImportsSatisfied()
    {
        // Add the imported resource dictionaries
        // to the application resources
        this.Resources.MergedDictionaries.Clear(); // in case of recompose
        logger.Info("Importing Styles...");
        foreach (ResourceDictionary r in Styles)
        {
            this.Resources.MergedDictionaries.Add(r);
        }
        logger.Info("Importing Views...");
        foreach (ResourceDictionary r in Views)
        {
            this.Resources.MergedDictionaries.Add(r);
        }

        if (!m_startupCommandsRun) // Don't run on recomposition
        {
            m_startupCommandsRun = true;
            // Run all the startup commands
            IList<IExecutableCommand> commands = ExtensionService.Sort(StartupCommands);
            foreach (IExecutableCommand cmd in commands)
            {
                logger.Info("Running startup command " + cmd.ID + "...");
                try
                {
                    cmd.Run();
                }
                catch (Exception ex)
                {
                    logger.Error("Exception while running command " + cmd.ID, ex);
                } 
                logger.Info("Startup command " + cmd.ID + " completed.");
            }
        }
    }

This is from the "Host" assembly. It imports the Styles and Views, and it adds them to the application resources. From that point, WPF takes care of applying Views to ViewModels because each View is a DataTemplate where the DataType is the ViewModel type itself. Anywhere WPF sees a ViewModel in the visual tree, it automatically applies the applicable DataTemplate.

link

answered 24 Sep '10, 20:48

Scott%20Whitlock's gravatar image

Scott Whitlock ♦♦
696262833
accept rate: 50%

1

Scott,

How does this work if you want 2 open instances of a view model? (2 documents with different content)

How does the WPF system know which instance of a viewmodel to hook to a view?

jim

(26 Sep '10, 08:19) jimmy

@jimmy: It's the other way around. ViewModels are created and show up in the logical tree (either MEF instantiates them or you do) and when WPF encounters it in the logical tree, but it isn't a real WPF object, it goes hunting for a DataTemplate that has the DataType attribute set to your ViewModel's Type. If it finds one, it works like the Factory Pattern: it creates a new View object based on your DataTemplate, sets the DataContext of it to your ViewModel, and puts the instantiated View into the visual tree.

(08 Oct '10, 17:28) Scott Whitlock ♦♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×3
×1

Asked: 24 Sep '10, 12:31

Seen: 4,907 times

Last updated: 08 Oct '10, 17:28

powered by OSQA