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 8808423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:37:11+00:00 2026-06-14T02:37:11+00:00

I am trying to set the ViewModel as the DataContext of the View using

  • 0

I am trying to set the ViewModel as the DataContext of the View using the following XAML code:

<UserControl.DataContext>
    <local:MyViewModel />
</UserControl.DataContext>

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MA_Resources/MA_ResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <local:MyViewModel x:Key="myViewModel" x:Name="myVM" />
    </ResourceDictionary>
</UserControl.Resources>

But, I observe that the view-model constructor is called twice. I understand the the view-model is getting instantiated twice in XAML and that I should set the DataContext as the StaticResource from the Resources. However, I am not able to figure out how to set the DataContext with the StaticResource.
I tried the following but it’s giving an exception:

<UserControl .... DataContext="{StaticResource myViewModel}" >

Please help me figuring out what should be the appropriate XAML code for assigning the DataContext.

  • 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-14T02:37:12+00:00Added an answer on June 14, 2026 at 2:37 am

    It is not possible to reference the static resource, if it is defined later in the xaml file. Therefore, you could do the following:

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MA_Resources/MA_ResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
    
            <local:MyViewModel x:Key="myViewModel" x:Name="myVM" />
        </ResourceDictionary>
    </UserControl.Resources>
    <UserControl.DataContext>
        <StaticResourceExtension ResourceKey="myViewModel"/>
    </UserControl.DataContext>
    

    I wonder why you want do define the ViewModel as a static resource. Personally, I would prefer the instantiation in the setter of the DataContext.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following: <FrameworkElement.Resources> <DataTemplate DataType={x:Type viewmodel:MainViewModel}> <view:MainBaseView /> </DataTemplate> </FrameworkElement.Resources>
I am trying to set the DataContext of the MainWindow to its ViewModel in
When trying to set DoMatchParen I get the following error: E492: Not an editor
I'm trying to do a simplest thing: bind a View to ViewModel, but for
In my Silverlight MVVM application, in the code-behind of a view, I set the
I'm developing an application in Silverlight2 and trying to follow the Model-View-ViewModel pattern. I
I'm trying to do the following using MVVM, Binding and AttachedProperty Create a ViewObj
I'm trying to bind my view to my view model without DataObjectProvider. The following
Im trying set the single table inheritance model type in a form. So i
Trying to set up a self signed certificate, for our intranet's web services site.

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.