clr-namespace:MyProject.ViewModels
There has to be a namespace called MyProject.ViewModels
that has a class Called MainPageViewModel
that is public and has a public parameterless constructor within the same assembly as ProjectDatabaseRebuilder.MainWindow
.
There is not.
If MyProject.ViewModels
exists in a referenced assembly, you must state so within the xmlns.
xmlns:vm="clr-namespace:MyProject.ViewModels;assembly=MyProject"
or some such. Honestly, it looks like you copypasted someone's example without realizing how these specialized xml namespaces work in WPF.
Something tells me the ultimate answer will be the following: xmlns:vm="clr-namespace:ProjectDatabaseRebuilder.ViewModels"
.
Please note that "namespace" and (as above) "assembly" mean namespaces and assemblies, and the xaml deserializer uses this information to locate types at runtime. If they are incorrect, things won't work.
This is trivial. You must have done something weird with your project, which might necessitate you start from scratch. Or, you can make a new project following my guide below, and then compare it bit by bit to yours to see where you went wrong.
First, create a new WPF application called MyWpfApplication.
Add a folder for Views and one for ViewModels. Add the shown VM code class and view UserControl:
In your code class, add the following:
namespace MyWpfApplication.ViewModels
{
class MainWindowViewModel
{
public string Text { get; set; }
public MainWindowViewModel()
{
Text = "It works.";
}
}
}
Your View is also simple:
<UserControl
x:Class="MyWpfApplication.Views.MainWindowView"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid>
<TextBlock
Text="{Binding Text}"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</UserControl>
And, in your Window, do essentially what you are attempting to do:
<Window
x:Class="MyWpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="clr-namespace:MyWpfApplication.Views"
xmlns:vm="clr-namespace:MyWpfApplication.ViewModels"
Title="DERP"
Content="{Binding DataContext, RelativeSource={RelativeSource Self}}">
<Window.Resources>
<DataTemplate
DataType="{x:Type vm:MainWindowViewModel}">
<v:MainWindowView />
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
</Window>
And, when run, you should see everything as expected:
Do this, then compare the working solution to your project. If you can't find any differences, you probably need to just scrap your solution and start fresh. Copy code, not files, into the new solution.