C# Can';在WPF MVVM中将视图绑定到ViewModel

C# Can';在WPF MVVM中将视图绑定到ViewModel,c#,wpf,mvvm,binding,C#,Wpf,Mvvm,Binding,我正在WPF中开发桌面应用程序,我希望遵循MVVM模式。我已经准备好了视图,是时候做一个viewmodel了。但由于某些原因,我无法将viewmodel绑定到视图。 我已经在视图的XAML中尝试过这一点: 不起作用,所以我在View类中尝试了以下方法: public主窗口() { this.DataContext=新的MainViewModel(); 初始化组件(); } 但它也不起作用。。。我试着在网上查找,但每个人都在做同样的事情 视图模型: class MainViewModel :

我正在WPF中开发桌面应用程序,我希望遵循MVVM模式。我已经准备好了视图,是时候做一个viewmodel了。但由于某些原因,我无法将viewmodel绑定到视图。 我已经在视图的XAML中尝试过这一点:


不起作用,所以我在View类中尝试了以下方法:

public主窗口()
{
this.DataContext=新的MainViewModel();
初始化组件();
}
但它也不起作用。。。我试着在网上查找,但每个人都在做同样的事情

视图模型:

class MainViewModel : INotifyPropertyChanged
    {
        public string BindingTest { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        public MainViewModel()
        {
            BindingTest = "test";
          }
    }
以及我如何绑定该属性:

<TextBlock Text="{Binding Path= BindingTest}" Padding="10"/>

以下是我的文件的外观:


如果要在XAML中设置DataContext,应执行以下操作:

<Window x:Class="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:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:viewModels="clr-namespace:AssemblyName.ViewModels"
        mc:Ignorable="d"
        Title="" Height="626" Width="1200" Background="#FFDEDF1A">
        <Window.DataContext>
             <viewModels:MainViewModel />
        </Window.DataContext>
        <!-- Your Code Here... -->
</Window>


程序集名称更改为项目名称

如果要在XAML中设置DataContext,应执行以下操作:

<Window x:Class="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:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:viewModels="clr-namespace:AssemblyName.ViewModels"
        mc:Ignorable="d"
        Title="" Height="626" Width="1200" Background="#FFDEDF1A">
        <Window.DataContext>
             <viewModels:MainViewModel />
        </Window.DataContext>
        <!-- Your Code Here... -->
</Window>


程序集名称更改为项目名称

从XAML中删除
DataContext=“ViewModels/MainViewModel”
。它毫无意义,而且在语法上也是错误的。
InitializeComponent()需要是构造函数中的第一项内容。请尝试放置DataContext=new MainViewModel();初始化组件()后@巴里欧·凯恩,那是错的。顺序一点也不重要。视图模型看起来怎么样?它有公共财产吗?你如何与他们结合?如果没有这些详细信息,我们将无能为力。请从XAML中删除
DataContext=“ViewModels/MainViewModel”
。它毫无意义,而且在语法上也是错误的。
InitializeComponent()需要是构造函数中的第一项内容。请尝试放置DataContext=new MainViewModel();初始化组件()后@巴里欧·凯恩,那是错的。顺序一点也不重要。视图模型看起来怎么样?它有公共财产吗?你如何与他们结合?没有这些细节我们无能为力。事实上这很有效。谢谢你,先生,还有@Clemens。实际上这很有效。谢谢你,先生,还有@Clemens。