I am having trouble accessing my ViewModel when working with my view.
I have a project named BankManagerApplication. Within that I have the various files associated with a new WPF application. I have created three seperate folders Model, ViewModel and View.
At the moment there is a UserModel class in the Model folder with the following fields;
namespace BankManagerApplication.Model
{
public class UserModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public double AccountBallance { get; set; }
}
}
a blank view in the View folder with just a grid inside;
<Window x:Class="BankManagerApplication.View.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindowView" Height="300" Width="300">
<Grid>
</Grid>
</Window>
and also a blank ViewModel in the ViewModel folder;
namespace BankManagerApplication.ViewModel
{
public class MainWindowViewModel
{
}
}
when i try to reference the ViewModel in my XAML like so;
<Window x:Class="BankManagerApplication.View.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindowView" Height="300" Width="300"
xmlns:viewmodel="clr-namespace:BankManagerApplication.ViewModel">
<Grid>
<viewmodel:MainWindowViewModel></viewmodel:MainWindowViewModel>
</Grid>
</Window>
i get the error
The name 'MainWindowViewModel does not exist in the namespace "clr-namespace:BankManagerApplication.ViewModel'
I have only just started learning WPF and this error is throwing me off before I have really begun
You cannot add it to a Grid control because it is not a UIElement. Your viewmodel will be the DataContext of your view:
<Window x:Class="BankManagerApplication.View.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindowView" Height="300" Width="300"
xmlns:viewmodel="clr-namespace:BankManagerApplication.ViewModel">
<Window.DataContext>
<viewmodel:MainWindowViewModel></viewmodel:MainWindowViewModel>
</Window.DataContext>
<Grid>
</Grid>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With