C# 无法更改DataContext WPF

C# 无法更改DataContext WPF,c#,wpf,mvvm,C#,Wpf,Mvvm,我试图使用MVVM模式,而不是将所有逻辑放在代码隐藏文件中,但我似乎无法让它工作 我的看法是: <Window x:Class="WpfApp4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.micro

我试图使用MVVM模式,而不是将所有逻辑放在代码隐藏文件中,但我似乎无法让它工作

我的看法是:

<Window x:Class="WpfApp4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp4"

    mc:Ignorable="d"
    xmlns:vm="clr-namespace:WpfApp4.ViewModel"       
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <vm:ViewModelMain />
</Window.DataContext>

<Grid>

</Grid>

我做错了什么?

VisualStudio有时会出现xaml警告和错误,有点脾气暴躁。尝试关闭窗口和/或清理并重建项目,或者如果所有其他操作都失败,请关闭所有窗口并重新启动visual studio

到底是什么不起作用?这会给我一个错误,告诉我ViewModelMain不属于命名空间WpfApp4。ViewModel vm:ViewModelMain。尝试将ViewModelMain类设为公共类并生成它。它确实运行,但是当鼠标悬停在xaml文件中的行上时,会收到一条消息,表明名称空间中不存在该名称空间。我应该忽略它吗?警告可能一直存在,因为我找不到代码有任何问题。请尝试重新启动窗口/项目/VS谢谢您的提示。它确实删除了警告,但事件处理程序似乎仍然显示在代码隐藏文件中。@Pelle,使用MVVM绑定命令时,您不使用事件处理程序。这是完全正确的。还有很多练习要做。。。谢谢你的帮助,你解决了我的问题!不过,我不得不经常重启VisualStudio,这真的很烦人。
namespace WpfApp4.ViewModel
{
    class ViewModelMain
    {
    }
}