How to add DataContext to User Control's XAML?

How to add DataContext to User Control's XAML?

I have a user control MyControl, it needs to be bound to its view model: MyControlViewModel. I have no problem to add the DataContext programmically in the constructor. But I was told this  is not allowed. It has to be done in the XAML. However, I have gotten the error in XAML

MyControlViewModel is in the project MyProject.ViewModel

<UserControl  .....
        <local:MyControlViewModel/>        // ERROR: The name MyControlViewModel does not exist in the namespace "clr-namespace:MyProject.ViewModel"  


      // Style stuff...


Anyone can help me?

Thanks a lot.
Who is Participating?
chuang4630Connect With a Mentor Author Commented:
I have tried and it does not work. I end it up by implementing it in code behind
Can you please add the assembly name and check. Open the assembly in object explorer  and check whether the class you are looking for is available.

<UserControl  .....
   xmlns:local="clr-namespace:MyProject.ViewModel, <assembly name>"
chuang4630Author Commented:
I have a right solution
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.