Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8598273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:06:51+00:00 2026-06-12T01:06:51+00:00

I am trying to hook up my viewmodel as a DataContext to my View.

  • 0

I am trying to “hook up” my viewmodel as a DataContext to my View. I am going with an application-wide one view-to-viewmodel scenario.

I like the idea of using the following method to attach (which it does successfully):

 <UserControl ......Window Stuff.......>
        <UserControl.DataContext >
             <vm:MyViewModel/>
         </UserControl.DataContext>
 </UserControl>

However the xaml editor places a purple squiggly line under <vm:MyViewModel/> and hovertext gives error

  "Cannot create instance of vm:MyViewModel"

But it correctly builds/runs my application and presents the data, which the method:

 <UserControl>
    <UserControl.Resources>
        <DataTemplate DataType="{x:Type vm:MyViewModel}">
             <vw:MyView/>
        </DataTemplate>
   </UserControl.Resources>
 </UserControl>

Does NOT present my data, although it appears to be acceptable code.

Commenting-out the DataContext reference is really a pain just for making edits to the controls manually. Am I doing something incorrectly/am I forgetting something?

Thanks in advance! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:06:53+00:00Added an answer on June 12, 2026 at 1:06 am

    There is likely something going on in the constructor of your view model that is preventing the model from being instantiated within Visual Studio.

    Try adding a return statement before anything is done in the view model’s constructor, rebuild and see if the error goes away.

    Here’s an extension method that will determine if the the current process is within visual studio:

    public static class Extensions
    {
        /// <summary>
        /// Extension method to determine if the current process is executing
        /// code within the Visual Studio designer or not.
        /// </summary>
        public static bool IsVisualStudioDesigner( this Process process )
        {
            return process.MainModule.ModuleName.Contains( "devenv.exe" );
        }
    }
    

    And then in your constructor:

    public ctor()
    {
      if ( Process.GetCurrentProcess().IsVisualStudioDesigner() )
        return;
    
      // do constructor stuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to hook a method with Application.ItemLoad event: private void ThisAddIn_Startup(object sender,
I am desperately trying and failing to hook CallWndProc using EasyHook. Intptr addr =
I am trying to retrieve messages for another application with a Windows hook. I
I'm trying to hook a CBT hook on Windows OSes. I'm currently using Windows
I'm new to using callback functions and struggling trying to hook some up through
My client is trying to hook to a usercontrols Loaded Event in the View
I'm trying to hook up a keyboard shortcut for my chrome extension. I'm using
Following is the code i am trying to hook with Telerik Combobox but it
I have been following the post below trying to hook my SubVersion installation to
I'm trying to hook up a checkbox to my View, but as soon as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.