0

I am new to WPF + MVVM architecture. In my application, I am trying to implement "DataContext" in the XAML itself as below

<Window x:Class="MyWPF.UI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:MyWPF.UI.ViewModel"
    Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <vm:Northwind_DataViewModel></vm:Northwind_DataViewModel>
    </Window.DataContext>

    <Grid>
    </Grid>

</Window>

I am getting "The name 'Northwind_DataViewModel' does not exists in the namespace 'clr-namespace:MyWPF.UI.ViewModel".

I have ViewModel file. Before using "DataContext", I built this application.

Could you please let me know what is wrong with my code?

thanks

AVG
  • 51
  • 1
  • 7
  • This might help - http://stackoverflow.com/questions/4590446/how-do-i-set-a-viewmodel-on-a-window-in-xaml-using-datacontext-property?rq=1 – Daniel Kelley Aug 04 '14 at 12:59

4 Answers4

0

The name 'Northwind_DataViewModel' does not exists in the namespace 'clr-namespace:MyWPF.UI.ViewModel".

Chances are, you compiler is right. There is no class named MyWPF.UI.ViewModel.Northwind_DataViewModel in your project. Make sure it exists, has a public, parameterless constructor, is not an inner class of something and not generic. If that's the case, it will work.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

In case namespace is declared in separate assembly from where your XAML resides, you have to provide assembly name as well.

Sample:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

Similarly, provide your assembly name where this namespace resides (Assuming assembly name is MyWPF.UI):

xmlns:vm="clr-namespace:MyWPF.UI.ViewModel;assembly=MyWPF.UI"
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
0

Thanks a lot your responses. Finally I got the solution. Actually, this project was placed in a shared folder. So, I moved it to my local drive (C:). Now, it is working fine without any issues.

I guess, in shared path / shared folder, it may not work. Please let me know if I am wrong.

thanks again.

AVG
  • 51
  • 1
  • 7
0

In order to solve this problem, you need to understand how the DataContext works. This answer contains a lot of hints and links to supporting tutorials:

ReSharper WPF error: "Cannot resolve symbol "MyVariable" due to unknown DataContext"

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305